Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I decrease a HP stat when touched?

Asked by 5 years ago

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

0
In the player that touched the part doesn't have any direct descendants that are called "leaderstats" so that errors RichCookieGamer 7 — 5y
0
Can you tell me where leaderstats is inside the player (example: plr.PlayerGui.leaderstats) RichCookieGamer 7 — 5y
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 VewixxPlayer 67 — 5y
0
game.Players.(player).leaderstats VewixxPlayer 67 — 5y
View all comments (9 more)
0
can You give me the code of the other script that adds the script? RichCookieGamer 7 — 5y
0
local plr = game.Players.LocalPlayer local leader = Instance.new("Folder") leader.Name = "leaderstats" leader.Parent = plr VewixxPlayer 67 — 5y
0
Do you have FilteringEnabled on? KingLoneCat 2642 — 5y
0
How do I check that? Sorry Im new at roblox scripting VewixxPlayer 67 — 5y
0
Go in workspace properties. Scroll down until you see "FilteringEnabled" Tell me if it has a check mark next to it. KingLoneCat 2642 — 5y
0
Yes it is activated VewixxPlayer 67 — 5y
0
Ok. I'll answer you KingLoneCat 2642 — 5y
0
Ok. Thanks! VewixxPlayer 67 — 5y
0
You can use remote event/functions RichCookieGamer 7 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Thank you, you helped me a lot! VewixxPlayer 67 — 5y
0
Glad to help. KingLoneCat 2642 — 5y
Ad

Answer this question