【问题标题】:How to run a local script when a brick is touched?触摸砖块时如何运行本地脚本?
【发布时间】:2021-05-13 09:24:39
【问题描述】:

所以,我希望在玩家客户端上运行一个脚本,该脚本基本上会激活 2 -1 饱和度颜色校正效果来反转屏幕上的颜色和仅屏幕上的颜色,但我只编写了几个月的脚本,而且我还没有准备好编写复杂的代码。

代码如下:

   game.Workspace.Five.Touched:Connect(function(hit)
        if hit.Parent == game.Players.LocalPlayer.Character then
            game.Lighting.inverted1.Enabled = true
            game.Lighting.inverted2.Enabled = true
        end
    end)

【问题讨论】:

  • LocalScript 位于何处?
  • 跟进@Jakye 的评论,看看docs for LocalScripts。包含有关脚本所在位置的信息很重要,因为您的脚本可能没有执行,因为它不在正确的位置之一。

标签: lua roblox


【解决方案1】:

将本地脚本放在 StarterPlayerScripts 中。

我以稍微好一点的方式重写了您的代码。将此代码放在本地脚本中

Lighting = game:GetService('Lighting')

game.Workspace.Five.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild('Humanoid') then
      print('Runned')
      Lighting.inverted1.Enabled = true
      Lighting.inverted2.Enabled = true
   end
 end)

建议使用game:GetService('Service name') 使用服务。 我还将if hit.Parent == game.Players.LocalPlayer.Character then 更改为if hit.Parent:FindFirstChild('Humanoid') then,因为它更简单并且代码运行速度比字符加载快,因此它会出错并停止运行。 与 GetService() API 参考链接:https://developer.roblox.com/en-us/api-reference/function/ServiceProvider/getService

【讨论】:

  • 您觉得我的回答有用吗?
【解决方案2】:

好的,我已经解决了,使用远程事件我让玩家接触了砖块并使用服务器来处理颜色校正启用

【讨论】:

  • 您应该将您的答案标记为正确,以便其他人知道。
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
相关资源
最近更新 更多