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

Why does my local variable have a error?(local Money)

Asked by 5 years ago

I'm not sure why this local variable has an error on it i assigned it a value so whats the problem?

01script.Parent.Touched:Connect(function(hit)
02local player = game.Players:GetPlayerFromCharacter
03local Money = player.leaderstats.Money -- my question,I just want to know whats causing the "local" error it says expected '(' and <string> what does it mean
04 
05if hit.Parent == game.Workspace.PartStorage then
06    wait(0.4)
07    Money.Value = Money.Value + hit.Value
08    hit:Destroy()
09 
10 
11end
12 
13    end
14 
15 
16 
17 
18end)

2 answers

Log in to vote
0
Answered by 5 years ago

You are not using GetPlayerFromCharacter correctly. It is a function and you need to call it passing it the instance to attempt to find the player. If no player is found then nil is returned.

1script.Parent.Touched:Connect(function(hit)
2    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- hit.Parent would be the model
3    if player then -- check that player is not nil
4        -- code
5    end
6end)

I hope this helps. Please comment if you have any other questions about this code.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

This should work. You aren't getting the player correctly and for that reason you can't get the leaderstats.

1local user = game.Players:GetPlayerFromCharacter(hit.Parent)
2        local stats = user:findFirstChild("leaderstats")
3 
4            if stats ~= nil then
5            local cash = stats:findFirstChild("Money")
6            cash.Value = cash.Value + numbervalue
0
thanks this helps me understand im not getting the player correctly thebananashetoldyou 31 — 5y

Answer this question