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

How Do I Update a players leaderstats from a script?

I'm Trying to check if the player is in a specific team and has a the value of X then they could use the button can anyone help

local CoinWorth = 50
Button.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

local Plr = workspace[hit.Parent.Name]

local Player = game.Players:GetPlayerFromCharacter(Plr)

if Player.Team == "Red" and Player.leaderstats.Coins.Value >= CoinWorth then

Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - CoinWorth

end

end

end)

1 answer

✓ Accepted answer

Hello. Here I fixed your Script and I added Debounce.

local CoinWorth = 50
local WaitTime = 5
local Debounce = false

function onHit(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerCharacter = game.Workspace[hit.Parent.Name]
		local Player = game.Players:GetPlayerFromCharacter(PlayerCharacter)
		if Player.Team.Name == "Red" and Player.leaderstats.Coins.Value >= CoinWorth and Debounce == false then
			Debounce = true
			Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - CoinWorth
			wait(WaitTime)
			Debounce = false
		end
	end
end

script.Parent.Touched:Connect(onHit)

You tried to check player's team by player.Team == "Red". This is like you are trying to check a team name like this.

if game.Teams.Red == "Red"" then
	print("True")
end

Which is incorrect. You can do: if player.Team.Name == "Red" or if player.Team == game.Teams.Red.

2 comments

0

Thanks :) For The Help I'm just a bit noobie when it comes to creating the script i know how i want to approach the problem just don't know the most eficient way to do it

0

No problem. Everyone was a beginner at a time.

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