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

I am having trouble getting the player from character?

Asked by 5 years ago

So here is the script for the part that I have. I have tried to make the script register when the player clicks so that it takes away from the value of cash overall in the part, but then also gives the player the equal amount of cash.

cash = 2000

script.Parent.ClickDetector.MouseClick:connect(function(hit)
    cash = cash - 100
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
    wait(1)
end)
if cash == 0 then
    script.Parent.Transparency = 1
    script.Parent.CanCollide = false
    script.Parent.ClickDetector.Destroy()
    wait(10)
    cash = 2000
    script.Parent.Transparency = 0
    script.CanCollide = true
    Instance.new("ClickDetector", script.Parent)
end

11:09:18.610 - Workspace.Part.Script:6: attempt to index local 'player' (a nil value)

That is the error I received when testing it out by clicking on the part.

Here is my leaderboard script that is referenced in the script in my part.

game.Players.PlayerAdded:connect(function(plr)
    local leader = Instance.new("Folder", plr)
    leader.Name = "leaderstats"
    local cash = Instance.new("IntValue", leader)
    cash.Name = "Cash"
end)

2 answers

Log in to vote
4
Answered by
Thernus 75
5 years ago
Edited 5 years ago

Hey! Actually, a ClickDetector immediately returns playerWhoClicked, which is the Player, and not the character. This code is basically trying to get the player from game.Players, which is not a valid character.

This should fix your issue:

cash = 2000

script.Parent.ClickDetector.MouseClick:connect(function(hit)
    cash = cash - 100
    local player = hit
    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
    wait(1)
end)
if cash == 0 then
    script.Parent.Transparency = 1
    script.Parent.CanCollide = false
    script.Parent.ClickDetector.Destroy()
    wait(10)
    cash = 2000
    script.Parent.Transparency = 0
    script.CanCollide = true
    Instance.new("ClickDetector", script.Parent)
end
0
Thanks, this worked; it's amazing it was that simple! LordIsaacofBargo 7 — 5y
0
Accept his answer if it helped RiskoZoSlovenska 378 — 5y
0
Sorry, I'm new. LordIsaacofBargo 7 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Not sure what you mean but this should work,if you are using a local script do this:


local cash = 2000 local player = game.Players.LocalPlayer --/ Gets the player script.Parent.ClickDetector.MouseClick:connect(function(hit) cash = cash - 100 --local player = game.Players:GetPlayerFromCharacter(hit.Parent) Nope player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100 wait(1) end) if cash == 0 then script.Parent.Transparency = 1 script.Parent.CanCollide = false script.Parent.ClickDetector.Destroy() wait(10) cash = 2000 script.Parent.Transparency = 0 script.CanCollide = true Instance.new("ClickDetector", script.Parent) end

If you are using a script, well... CHANGE (ik it sucks)

Answer this question