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

When I touch the part why doesn't it give me any coins?

Asked by 5 years ago
Edited by DanzLua 5 years ago

Here is the script where I want it to give you 5 coins when you touch the part

local player = game.Players:GetPlayerFromCharacter()
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        player.leaderstats:FindFirstChild("Coins")
        Coins.Value = 5
    end
end)

And then the leaderboard script

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("IntValue",player)
    stats.Name = "leaderstats"

    money = Instance.new("IntValue",stats)
    money.Name = "Coins"
end)



1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

This isn't working because you are setting player outside of the touched event and with no parameter, which results in nil.

And Coins wasn't set to anything.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local Coins=player.leaderstats:FindFirstChild("Coins")
        Coins.Value = 5
    end
end)
Ad

Answer this question