Sell place not working when it should? (or its just me)

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

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.

2 Likes

Confusion 100

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

3 Likes

Ohhhh, yeah I forgot to switch that out when I was trying to test on local scipts but it gives me the same error

1 Like

Yup you’re right, it can all be run in a server script. Thanks for clarifying!

Change it to a Server Script instead & it should work just fine :wink: (As long as you’ve defined all of your leaderstats properly, otherwise do show the console for any Output errors)

1 Like

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

You never implemented a conditional check, use the code I referenced earlier and that should work properly

If you don’t implement a sanity check that the Part’s Parent touched was a Player’s Character Model, it will result as an error

1 Like

It still isnt working I added the “if player then” but it just doesnt do anything or show any error codes

1 Like

Although I put a print in there and it does print it out just doesnt dont anything else

1 Like

Hm, can you check for print statements then?

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

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?

What did plr print out as? Also where is this Part exactly located in?

Just saw the edit, could it be possible that you reference your leaderstats object in the wrong area or 1 of your power/coins variables?

It printed out “musman65” the part is located directly on the sell circle decal here is a picture

Upto which line does it print out? And is the power 0+?

It prints out eveyrthing but what do you mean by 0+?

The issue is somewhere lying between these lines then

Do some debugging & uh, print what everything is supposed to be?

I mean is the power more than 0

Edit: If you change the power value from the client I don’t think it will work.

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