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

Can someone help me fix my leaderstats script to check if the player has a leaderstats value of <1?

Here is my script:

script.Parent.touched:Connect(function(player)
	if player.leaderstats.Stage.Value <1 then
		local PlayerPoints = player.leaderstats.Stage
		PlayerPoints.Value = PlayerPoints.Value + 1 else
			print("Too high value")
	end
end)

the error is at

if player.leaderstats.Stage.Value <1 then

The error im getting is leaderstats is not a valid member of mesh parts

3 comments

0

Maybe you capitalized the L in leaderstats.

0

what is player in this scenario? Is it game.Players.LocalPlayer

0

OHHHH I UNDERSTAND WAIT

2 answers

✓ Accepted answer

Okay so there are a couple of things that you messed up on but don't worry I'll explain:

So the first thing is the touched event is actually with a capital T so it's

script.Parent.Touched:Connect(function(player)

The next thing is that player is not actually player in this scenario. The touched event returns the part that touched the script.Parent. So in this case it was the humanoid not the player.

So in order to get the player you need to get the player from the character. Do this:

script.Parent.Touched:Connect(function(partThatWasHit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(partThatWasHit.Parent) 
    	if player.leaderstats.Stage.Value <1 then
	        local PlayerPoints = player.leaderstats.Stage
	        PlayerPoints.Value = PlayerPoints.Value + 1 else
	        print("Too high value")
	end
end)

1 comment

0

It worked great! Thx C:

The Touched function passes the part that was hit, not the player. There is however a way to get the player from this part using

GetPlayerFromCharacter

Example

local playerService = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent then
		if playerService:GetPlayerFromCharacter(hit.Parent) then
			print("A Player has touched this part")
		end
	end
end)

Now, let's fix your code

local playerService = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent then
		if playerService:GetPlayerFromCharacter(hit.Parent) then
			local playerPoints = player.leaderstats.Stage
			if playerPoints ~= nil and playerPoints.Value < 1 then
				playerPoints.Value = playerPoints.Value + 1
			end
		end
	end
end)

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