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

Value status doesn't change?

Asked by 3 years ago
Edited 3 years ago

(This is a normal script, not a LocalScript)

Okay so, I'm trying to make a script so that you can click a gas can and it changes your value, if your value is changed, you can fuel something. I put this script in StarterCharacterScripts so that it creates a value when the player joins. Code:

local Player = game.Players.LocalPlayer

local Value = Instance.new("BoolValue")

Value.Parent = Player
Value.Name = "HasGas"
Value.Value = false

But, the problem isn't with the value. I'm trying to make the value change when you click a button, here's my code:

local Player = game.Players.LocalPlayer
local value = Player:WaitForChild("HasGas")

script.Parent.ClickDetector.MouseClick:Connect(function()
value.Value = true
end)

I get an error with this; here's the error: "attempt to index nil with WaitForChild"

Did I type or do something wrong? Thanks, and if you can help, that would be greatly appreciated.

0
Is this second script a Script or a LocalScript? DragonSpawner12 61 — 3y
0
Script, its in a part CoolBlueJay000 48 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Several things wrong here. First, you can't use localplayer in a server script. Second, i'd recommend putting the first script into serverscript service.

Try this script out and see if it works

put this script in serverscriptservice

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:Connect(function(chr)
        local Value = Instance.new("BoolValue")
        Value.Parent = plr
        Value.Name = "HasGas"
        Value.Value = false
    end)
end)




put this script in button

local value = Player:WaitForChild("HasGas")

script.Parent.ClickDetector.MouseClick:Connect(function(player)
local value = Player:WaitForChild("HasGas")
value.Value = true
end)
Ad
Log in to vote
0
Answered by 3 years ago

Your problem is that you are creating the value on the client (local script), and because the server can't see the stuff that the client is making, it is returning nil. To fix this, you should make the bool value on the server (in a Script).

Answer this question