禁用激光而不是启用激光? (ROBLOX Lua)

时间:2018-07-23 20:25:27

标签: lua roblox

嘿stackoverflow社区。因此,我开始在ROBLOX中制作“大亨”。我收集了几个自定义模型,但是其中一个遇到了问题。基本上,我希望为每个玩家群提供一个激光门。我的问题是,在游戏开始时启用了来自激光门的激光。我个人希望它们在一开始就被禁用,以便以后可以由播放器手动启用。我为此苦苦挣扎的原因是因为我不是编写脚本的人,但是,我曾尝试解决它,但没有成功。

script.LazerGateLocalScript.LazerGate.Value = script.Parent -- setting a 
value
local configs = script.Parent.Configurations
local lazers = script.Parent.LazerGate -- making it easier to access this 
part
local lazersOn = true -- so we know if the lasers are on or off

for i, node in pairs(lazers:GetChildren()) do
for i, component in pairs(node:GetChildren()) do
    if component.Name == "Emitter" then
        local lazerBeam = component:FindFirstChild("ParticleEmitter")
        local lazerColor = configs.LazerColor.Value
        lazerBeam.Color = ColorSequence.new(lazerColor, lazerColor) -- Setting the lazer color to the lazer color found in the configurations of this model
    elseif component.Name == "Diode" then
        component.BrickColor = configs.DiodeColor.Value
        local diodeSparkle = component:FindFirstChild("ParticleEmitter")
        diodeSparkle.Color = ColorSequence.new(Color3.new(255,255,255), configs.DiodeSparkleColor.Value)
    elseif component.Name == "Lense" then
        component.BrickColor = configs.DiodeColor.Value
    end
end
end

script.Parent.KillBrick.PointLight.Color = configs.LazerColor.Value

game.Players.PlayerAdded:connect(function(player) -- when a player is added
player.CharacterAdded:connect(function() -- and whenever their character is loaded then
    local newScript = script.LazerGateLocalScript:Clone() -- clone the local script
    newScript.Parent = player.PlayerGui -- and put it in the player's playerGui
end)
end)




function lazerToggle(OnOff) -- this funtionc simply changes whether the lasers are on or off
for i, node in pairs(lazers:GetChildren()) do
    for i, component in pairs(node:GetChildren()) do
        if component.Name == "Emitter" then
            local lazerBeam = component:FindFirstChild("ParticleEmitter")
            if OnOff == true then
                lazerBeam.Enabled = true
            else
                lazerBeam.Enabled = false
            end
        end
    end
end
end

script.Parent.KillBrick.Touched:connect(function(obj)
if obj.Parent and obj.Parent:FindFirstChild("Humanoid") and lazersOn == true then
    obj.Parent:BreakJoints()
end
end)

script.Parent.LazerToggle.OnServerEvent:connect(function() -- when an event is fired  then see if we are to open or close the lid and set a value to do so
if lazersOn == true then
    lazerToggle(false)
    wait(1.5)
    lazersOn = false
else
    lazerToggle(true)
    wait(0.5)
    lazersOn = true
end
end)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:connect(function() -- if the player chicks then ...
if script.LazerGate.Value and mouse.Target.Name == "LazerButton" and mouse.Target.Parent.Name == "LazerGateSection" then -- if they click the laser gates button
    script.LazerGate.Value.LazerToggle:FireServer() -- fire an event that the script will use
end
end)

再一次,我的问题是我希望禁用激光器,而不是在一开始就启用。任何帮助将不胜感激。另外,我非常清楚,我将对此帖子(xD)表示敬意。但我将非常感谢您的帮助!

预先感谢, E.W

1 个答案:

答案 0 :(得分:1)

从代码告诉我的角度来看,这应该可行:

    script.LazerGateLocalScript.LazerGate.Value = script.Parent
    local configs = script.Parent.Configurations
    local lazers = script.Parent.LazerGate
    local lazersOn = false

    for _, node in pairs(lazers:GetChildren()) do
        for _, component in pairs(node:GetChildren()) do
            if component.Name == "Emitter" then
                local lazerBeam = component:FindFirstChild("ParticleEmitter")
                local lazerColor = configs.LazerColor.Value
                lazerBeam.Color = ColorSequence.new(lazerColor, lazerColor)
            elseif component.Name == "Diode" then
                component.BrickColor = configs.DiodeColor.Value
                local diodeSparkle = component:FindFirstChild("ParticleEmitter")
                diodeSparkle.Color = ColorSequence.new(Color3.new(255,255,255), configs.DiodeSparkleColor.Value)
            elseif component.Name == "Lense" then
                component.BrickColor = configs.DiodeColor.Value
            end
        end
    end

    script.Parent.KillBrick.PointLight.Color = configs.LazerColor.Value
    game.Players.PlayerAdded:connect(function(player)
        player.CharacterAdded:connect(function()
            local newScript = script.LazerGateLocalScript:Clone()
            newScript.Parent = player.PlayerGui
        end)
    end)

    function lazerToggle(OnOff)
        for _, node in pairs(lazers:GetChildren()) do
            for _, component in pairs(node:GetChildren()) do
                if component.Name == "Emitter" then
                    local lazerBeam = component:FindFirstChild("ParticleEmitter")
                    if OnOff then
                        lazerBeam.Enabled = true
                    else
                        lazerBeam.Enabled = false
                    end
                end
            end
        end
    end

    script.Parent.KillBrick.Touched:connect(function(obj)
        if obj.Parent and obj.Parent:FindFirstChild("Humanoid") and lazersOn == true then
            obj.Parent:BreakJoints()
        end
    end)

    script.Parent.LazerToggle.OnServerEvent:connect(function()
        if lazersOn == true then
            lazerToggle(false)
            wait(1.5)
            lazersOn = false
        else
            lazerToggle(true)
            wait(0.5)
            lazersOn = true
        end
    end)
    lazerToggle(false);

上面的代码进入第一个脚本,即服务器端脚本,该脚本具有名为LazerGateLocalScript的子级。

    local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()
    mouse.Button1Down:connect(function()
        if script.LazerGate.Value and mouse.Target.Name == "LazerButton" and mouse.Target.Parent.Name == "LazerGateSection" then
            script.LazerGate.Value.LazerToggle:FireServer()
        end
    end)

然后上面的代码进入本地脚本,该本地脚本是名为LazerGateLocalScript的服务器脚本的子代

请问是的,我认识RobloxLua。