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

How can I get this currency pickup to work?

Asked by 4 years ago

I am trying to make a currency pickup system.

When I were to touch the object, it should increase the leaderboard value for the player's coins via leaderstats, but it doesn't. I did not receive any error messages, so I am completely left in the dark.

The following code is in a local script.

coin = script.Parent

coin.Touched:Connect(function(player)
    if coin.AlreadyCollected.Value == false then
        local LocalPlayer = game.Players.LocalPlayer
        local gold = LocalPlayer.leaderstats.Gold.Value

        gold = gold + 1
    end
end)

Any help will be appreciated! :)

2 answers

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

Just a heads up that LocalScripts only affect that one person, or the client! I'll attempt to remake your script here;

--Script
local coin = script.Parent

coin.Touched:Connect(function(hit) --.Touched only returns the part that touched it, not the player
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Checks if the hit's parent is a character
    if plr and not coin.AlreadyCollected.Value then --If it is, and if the bool is false then
        coin.AlreadyCollected.Value = true --Makes it so it can't be collected again
        local gold = plr.leaderstats.Gold.Value
        gold = gold + 1 --Adds the gold
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago

Localscripts only affect the client, and don't even work in the workspace. Get the character using game.Players:GetPlayerFromCharacter(player.Parent), and then add it to the player's currency through that.

0
"player" comes from your touched object btw itchymoonfire 179 — 4y

Answer this question