Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Value status doesn't change?

(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.

2 comments

0

Is this second script a Script or a LocalScript?

0

Script, its in a part

2 answers

✓ Accepted answer

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)

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).

This discussion is preserved as it was. New answers, comments, and votes are closed.