Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I get this script to detect changes in values?

Asked by 8 years ago

The script works in the sense that when you click the GUI that the script is contained it, it teleports you and gives you your weapons of choice. However, if you were to change those weapons, the script wouldn't detect that, and would give you the old weapons instead.

I have no idea why this is happening. Can someone please help me?

Here's the script:

local Player = script.Parent.Parent.Parent.Parent.Parent
local W1 = script.Parent.Parent.Parent:WaitForChild("Wep1")
local W2 = script.Parent.Parent.Parent:WaitForChild("Wep2")
local GamePlaying = game.ServerScriptService.MainGame:WaitForChild("Playing")
local Weapon1 = game.ServerStorage.Weapons[W1.Value]:clone()
local Weapon2 = game.ServerStorage.Weapons[W2.Value]:clone()

function onButtonClicked()
if GamePlaying.Value == true then
    local torso = Player.Character.Torso
    local spawns = {}
    for i,v in ipairs(game.Workspace.Map.Spawns.Team1:GetChildren()) do
        if v.Name == "Spawn" then
            table.insert(spawns,v)
        end
    end
    Weapon1.Parent = Player.Backpack
    Weapon2.Parent = Player.Backpack
    torso.CFrame = spawns[math.random(1,#spawns)].CFrame
 + Vector3.new(0,4,0)
    script.Parent.Parent.Parent:remove()
end
end

script.Parent.MouseButton1Click:connect(onButtonClicked)

1 answer

Log in to vote
0
Answered by 8 years ago

This is probably happening because you're only making one copy of the old weapons, and referencing those. A fix would be to create the variables in the function, so you're using up-to-date copies of the weapons, rather than old copies.

local Player = script.Parent.Parent.Parent.Parent.Parent
local W1 = script.Parent.Parent.Parent:WaitForChild("Wep1")
local W2 = script.Parent.Parent.Parent:WaitForChild("Wep2")
local GamePlaying = game.ServerScriptService.MainGame:WaitForChild("Playing")


function onButtonClicked()
if GamePlaying.Value == true then
    local torso = Player.Character.Torso
    local spawns = {}
    for i,v in ipairs(game.Workspace.Map.Spawns.Team1:GetChildren()) do
        if v.Name == "Spawn" then
            table.insert(spawns,v)
        end
    end
    local Weapon1 = game.ServerStorage.Weapons[W1.Value]:clone()
    local Weapon2 = game.ServerStorage.Weapons[W2.Value]:clone()
    Weapon1.Parent = Player.Backpack
    Weapon2.Parent = Player.Backpack
    torso.CFrame = spawns[math.random(1,#spawns)].CFrame
 + Vector3.new(0,4,0)
    script.Parent.Parent.Parent:remove()
end
end

script.Parent.MouseButton1Click:connect(onButtonClicked)
0
Thanks so much! CoolJohnnyboy 121 — 8y
Ad

Answer this question