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

Can I carry variables through scripts?

Ok so, I have this script that makes a leader board:

game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new("IntValue", plr)
	stats.Name = "leaderstats"
	local points = Instance.new("IntValue",stats)
	points.Name = "Copper"
	points.Value = 0
end)

I then want to make a button that when you click it you get 1 copper: here it is:

function onClicked(playerWhoClicked)
	script.Parent.BrickColor = BrickColor.DarkGray()
	wait(6)
	points.Value = +1
	script.Parent.BrickColor = BrickColor.new("Reddish brown")
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Upon starting it gave me the error of: ServerScriptService.Script:2: attempt to index global 'points' (a nil value) I think this is because the variable from my fist scrip is a local and not a global therefore I cannot transfer it, all this caused the block to be un-clickable upon removing the local from that scrip it has no visible change, the button was still un-clickable Please help!

P.s if you find any other errors in my coding please notify me!

1 comment

0

I edited my answer. Please don't ask the same question twice, in the future.

1 answer

Just find the points in the player

You get the playerWhoClicked, now find the value inside them and change it.

function onClicked(playerWhoClicked)
	script.Parent.BrickColor = BrickColor.DarkGray()
	local points = playerWhoClicked.leaderstats.Points
	points.Value = points.Value+1
	script.Parent.BrickColor = BrickColor.new("Reddish brown")
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

EDIT

Because you changed the name of points in the script where you create the points, here.

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new("IntValue", plr)
    stats.Name = "leaderstats"
    local points = Instance.new("IntValue",stats)
    points.Name = "Copper"
    points.Value = 0
end)

You would have to account for this.

Final Script

function onClicked(playerWhoClicked)
	script.Parent.BrickColor = BrickColor.DarkGray()
	local points = playerWhoClicked.leaderstats.Copper--Changed to Copper
	points.Value = points.Value+1
	script.Parent.BrickColor = BrickColor.new("Reddish brown")
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Side Note

Please, in the future, don't ask another question if my or someone else's question didn't help you. Instead, tell them so they can correct their error. Thank you.

Good Luck

if I helped, please don't forget to accept my answer.

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