Script Support Needed

Hello, I am trying to make so when you step on a part you sell the stone you have and gets coins however it gives me an error leaderstats is not a valid member of Model “Workspace.slllJakob” How do I fix this?

local debounce = false
local player = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			local Stone = player.leaderstats.Stone.Value
			local Cash = player.leaderstats.Cash.Value
			
			
			print(hit.Parent)
			debounce = false

		end	
	end
end)

leaderstats isnt defined in the player

edit: your not accessing local player correctly

here is the new script

local debounce = false
local player = game:GetService("Players").LocalPlayer

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			local Stone = player.leaderstats.Stone.Value
			local Cash = player.leaderstats.Cash.Value
			
			
			print(hit.Parent)
			debounce = false

		end	
	end
end)

Now it says Workspace.SellPart.SellScript:9: attempt to index nil with ‘leaderstats’

Is this in a server script or local script?

It is a serverscript, should it be in a localscript?

local debounce = false

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not debounce then
        debounce = true
        local Stone = player.leaderstats.Stone.Value
        local Cash = player.leaderstats.Cash.Value
        
        task.wait(0.25)
        debounce = false
	end
end)
1 Like

No its fine hold on let me write a script

edit : nvm

Yes, it should be a LocalScript to have better detection to the LocalPlayer.

No, it needs to be a server script. Changing leaderstats on the client is pointless. (Unless you mean you wanted to use remoteevents)

2 Likes

Well thats arguable but he doesnt need remote events to change the leaderstats when he does it through the server

@jakob667c’s Script should work correctly, so basically he is accessing the player using the GetPlayerFromCharacter function then passing what it hit

Also in this script, just incase you didn’t know, setting the Stone and Cash variables will not directly set the leaderstats, because I just noticed the script doesn’t change any leaderstats yet.

1 Like

Why? It changes the values in another scirpt I have

You can do this:

Thing.Value = 5
print(Thing.Value) --> 5

But not this:

print(Thing.Value) --> 5
local Val = Thing.Value
Val = 10
print(Thing.Value) --> Still 5?

It prints 5 but it doesn’t show 5 in the scoreboard?

Edit: nvm fixed it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.