It doesnt find the leaderstats

so, basically i was making a surfacegui for a buying thing:
Screenshot 2024-08-22 133810
this is the script:

it makes an error that leaderstats was not found, and idk what to do

2 Likes

TextButtons do not work in server scripts (the white color ones) Remove it entirely and replace it with a click detector. That way it should be working. Put the script under the clickdetector as well

local price = 3000

script.Parent.MouseClick:Connect(function()
   if player.leaderstats.Cash.Value >= price then
     player.leaderstats.Cash.Value -= price
   end
end)

but it works even in surfacegui.textlabel right?

yes the surface gui’s text label can be edited with a server script, so you can place it there.

Edit: If you’re gonna place it in the text label then make sure the path for the click detector is different. Like if the click detector is under the part and the script is under the text label then it would be like script.Parent.Parent.Parent.ClickDetector.MouseClick

1 Like

try this:

script.Parent.MouseClick:Connect(function(plr)
    local ls = plr.leaderstats
    if not ls then ls = Instance.new("Folder", plr) end
    
    --Rest of code goes in here
end)

By default leaderstats folder isn’t present inside Player object

You could also add a script inside serverscriptservice to create a leaderstats folder upon joining the game:

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"
    local cash = Instance.new("NumberValue")
    cash.Parent = leaderstats
    cash.Name = "Cash"
end)

this is what ive done:

update: i changed to mouseclick and it works

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