Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to retrive data from leaderstats?

Okay so i'm learning to script and just trying things out to get started and what i've tried to do here is to get the Gold.Value from Leaderstats and check if the player has enough gold, i've had a go but the function doesn't seem to run? and I get nil for gold?

tree = game.ReplicatedStorage.Pinetree
button = script.Parent
player = game:GetService("Players")
leaderstats = player:FindFirstChild("leaderstats")
localplayer = game.Players:GetPlayerFromCharacter(character)



function onTouched (hit)
	
	if hit.Parent:FindFirstChild("Humanoid") and gold > 10 then
		script.Parent:Destroy()
			tree:Clone().Parent = game.Workspace
		else 
		print "Get more money!"
		end
		
		
	end

button.Touched:Connect(onTouched)

I'll learn from my mistakes, it's all about trying and failing i suppose. :)

1 answer

Here is the fixed script with explanation comments:

local tree = game.ReplicatedStorage:WaitForChild("Pinetree") --gets pinetree
local button = script.Parent --gets button

local function onTouched(hit) --makes a function
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --gets player from the param hit
	if hit.Parent:FindFirstChild("Humanoid") and plr.leaderstats.Gold.Value > 10 then --checks if the button detects the humanoid and if gold is greater than 10
		local treeClone = tree:Clone() --makes a new variable here for the cloned tree
		treeClone.Parent = workspace --sets the clone's parent
		button:Destroy() --destroys the parent button
	else --if one, or both of the statements are false
		print("Earn more money!") --prints: Earn more money!
	end
end

button.Touched:Connect(onTouched) --detects if the player steps on a button.

Hoped it helped out! If there are any errors let me know.

This discussion is preserved as it was. New answers, comments, and votes are closed.