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 4 years ago

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

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter
local 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

if hit.Parent == game.Workspace.PartStorage then
    wait(0.4)
    Money.Value = Money.Value + hit.Value
    hit:Destroy()


end 

    end 




end)

2 answers

Log in to vote
0
Answered by 4 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.

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

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

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 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.

local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        local stats = user:findFirstChild("leaderstats")

            if stats ~= nil then 
            local cash = stats:findFirstChild("Money")
            cash.Value = cash.Value + numbervalue
0
thanks this helps me understand im not getting the player correctly thebananashetoldyou 31 — 4y

Answer this question