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

I have a problem when getting a player from character, How to fix it?

Asked by 4 years ago

I want to change player leaderstats when touching part and error error code:

09:29:18.457 - Workspace.Finish.Part.Script:4: attempt to perform arithmetic (add) on userdata and number

09:29:18.459 - Stack Begin

09:29:18.460 - Script 'Workspace.Finish.Part.Script', Line 4

09:29:18.461 - Stack End

This is the script:

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        player.leaderstats.Finish = player.leaderstats.Finish + 1
        player.data.cash = player.data.cash + 50
    end
end)

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Easy quick fix! Add .Value to access its value!

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        player.leaderstats.Finish.Value = player.leaderstats.Finish.Value + 1
        player.data.cash.Value = player.data.cash.Value + 50
    end
end)

Ad

Answer this question