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

How to take a variable out of my first script and put it through my second script?


game.Players.PlayerAdded:connect(function(plr) 
	
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
    local clicks = Instance.new("IntValue", stats)
	clicks.Name = "Clicks"
	clicks.Value = 0
	clicks.Parent = stats
	
	local level = Instance.new("IntValue", stats)
	level.Name = "Rank"
	level.Value = 0
	level.Parent = stats
	
end)


first script



function onClick()
	--where I want to put my variable
end
script.Parent.MouseClick:connect(onClick)


second script . . I'm trying to make a clickable object thaat when you click it, the variable "clicks" goes up

1 comment

0

You can do what the people below suggested, or you can use _G (global variable). But using _G isn't that good and in this case there is no point of using it.

2 answers

When you use the MouseClick event, you can insert a parameter that tells you who clicked the object. In your onClick function we can insert a variable called "player" into the parentheses.

function onClick(player)
    --where I want to put my variable
end
script.Parent.MouseClick:connect(onClick)

Now whenever the object is clicked the player who clicked it will be stored in the variable "player".

Since you have added a value (clicks value) into the player directly from the server, we can access that value without a wall blocking us. Since we already have the player who clicked the object, we can just change their clicks variable in the function.

function onClick(player)
    player.leaderstats.clicks.Value = player.leaderstats.clicks.Value + 1
end
script.Parent.MouseClick:connect(onClick)

(you can make clicks into a variable if you want) This should work! :D

2 comments

0

Thank you, I didn't realize I can just put leaderstats!

0

No problem! (:

MouseClick returns the player who clicked.

function onClick(player)
	if player.leaderstats:FindFirstChild("Clicks") then 
		local ClicksValue = player.leaderstats:FindFirstChild("Clicks").Value
		ClicksValue = ClicksValue + 1 -- you can change the amount it adds
	end
end
script.Parent.MouseClick:connect(onClick)

2 comments

0

The script works, but the number doesn't go up

0

That's because you stored the Value in a variable, not the Reference. There's a difference. If you want to increment the value, you have to store the "Clicks" object in a variable, not the .Value of the object. Then do Clicks.Value = Clicks.Value + 1.

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