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)
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
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)