I am working on a sell area when you touch a part, it will check how much powr you have and convert it into coins but intead of doing that it just gives me this error: Workspace.Sell.Script:5: attempt to index nil with ‘leaderstats’
script.Parent.Part.Touched:Connect(function(hit)
local plr = game.Players.LocalPlayer
local power = plr.leaderstats.Power
local coins = plr.leaderstats.Coins
coins.Value += power.Value
power.Value = 0
end)
You need a local sided and server script for this to work, you will have to use a remote event that’s fired from the player’s client to the server, and the server converts power to coins.
You can’t reference the LocalPlayer in a server script, it just isn’t possible due to client-server replication
Call the GetPlayerFromCharacter() function instead which would detect if there’s a valid Player or not
local DB = false
script.Parent.Part.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and not DB then
DB = true
local power = plr.leaderstats.Power
local coins = plr.leaderstats.Coins
coins.Value += power.Value
power.Value = 0
wait(1)
DB = false
end
end)
@Bou_Baker A RemoteEvent is not necessary for this situation, you can easily get the Player from calling this function alone
Change it to a Server Script instead & it should work just fine (As long as you’ve defined all of your leaderstats properly, otherwise do show the console for any Output errors)
But it is a server script, it was working before but it stopped suddenly
here is the code again with the new function just to make sure:
script.Parent.Part.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local power = plr.leaderstats.Power
local coins = plr.leaderstats.Coins
coins.Value += power.Value
power.Value = 0
end)
local DB = false
print("This part works 100%")
script.Parent.Part.Touched:Connect(function(hit)
print("Event fired")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
print(plr)
if plr and not DB then
print("Found a player, now increasing Coins")
DB = true
local power = plr.leaderstats.Power
local coins = plr.leaderstats.Coins
coins.Value += power.Value
power.Value = 0
wait(1)
DB = false
else
warn("Something happened while attempting to get a player, or the debounce is still ongoing")
end
end)
Everything printed out except for the “Found a player, now increasing Coins”
Edit: Wait nono it printed out once but didnt increase my coins for some reason?
Here is the tool the gives you power if you click it (local script)
local debounce = false
local Tool = script.Parent
Tool.Activated:Connect(function(hit)
if debounce == false then
local plr = game.Players.LocalPlayer
plr.leaderstats.Power.Value += 100
debounce = true
wait(.5)
debounce = false
end
end)
and @JackscarIitt , I put prints between each line in the designated area and it printed eveyrthing