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 decrease a HP stat when touched?

I am doing my own health system for my game and I am having trouble with decrasing a player's HP. This script is in the part that you need to touch to decrease HP.

debounce = false
function onTouched(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if (humanoid ~= nil) then
		if not debounce then
				if script.Parent.Name == "Ball" then
				debounce = true
				local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
				plr.leaderstats.HP.Value = plr.leaderstats.HP.Value-1
				wait(5)
				debounce = false
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

It works in roblox studio but not in the real game. Everything is in the place it should be and have these names. In the game, it returns: "leaderstats is not a valid member of player" (line 9)

Thanks

13 comments

0

In the player that touched the part doesn't have any direct descendants that are called "leaderstats" so that errors

0

Can you tell me where leaderstats is inside the player (example: plr.PlayerGui.leaderstats)

0

But I have another script that creates a Folder inside the player with that name. Also the leaderstats are showing in the player list, but not changing when touching it

0

game.Players.(player).leaderstats

0

can You give me the code of the other script that adds the script?

0

local plr = game.Players.LocalPlayer local leader = Instance.new("Folder") leader.Name = "leaderstats" leader.Parent = plr

0

Do you have FilteringEnabled on?

0

How do I check that? Sorry Im new at roblox scripting

0

Go in workspace properties. Scroll down until you see "FilteringEnabled" Tell me if it has a check mark next to it.

0

Yes it is activated

0

Ok. I'll answer you

0

Ok. Thanks!

0

You can use remote event/functions

1 answer

✓ Accepted answer

Hi Vewixx,


#The Problem

The reason that you get that error in the game is because you have FilteringEnabled on. FilteringEnabled disables direct communication between server and client. So, when you parent the leaderstats, you parent it to the client. However, you are accessing it from the server. So, you can't access it from the server if it's located in the client.


#The Solution

I would suggest using RemoteEvents or RemoteFunctions to make this work. So, put a script on the client side, maybe under leaderstats, that gets activated upon firing a RemoteEvent/RemoteFunction. Then, just make the script change the value.


#Resources

If you don't know how Remote Events work, go here.


##Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

2 comments

0

Thank you, you helped me a lot!

0

Glad to help.

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