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

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

Asked by 5 years ago
Edited 5 years ago

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

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. valchip 789 — 5y

2 answers

Log in to vote
1
Answered by
Pojoto 329 Moderation Voter
5 years ago
Edited 5 years ago

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

0
Thank you, I didn't realize I can just put leaderstats! iPenguin012 0 — 5y
0
No problem! (: Pojoto 329 — 5y
Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago

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)
0
The script works, but the number doesn't go up iPenguin012 0 — 5y
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. chomboghai 2044 — 5y

Answer this question