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

LocalScripts can get Character models but nothing inside the model?

Asked by 5 years ago

I was trying to get the Humanoid.Health value in a localscript for a healthbar, I found that localscripts cannot get descendants of the character model. Any help with getting humanoid values in a local script?

1 answer

Log in to vote
0
Answered by
blazar04 281 Moderation Voter
5 years ago

You can change Humanoid.Health in a localscript, but if FilteringEnabled is on, it will only change the health for that player. For all of the other players, the player's health would be at 0. FilteringEnabled prevents one client(one player) from making changes to the whole server, which is why changing the health value with a LocalScript only appears on the client's screen. You want FilteringEnabled on to prevent exploits, so you will need to use RemoteEvents to solve this problem.

Your LocalScript would need to have a character variable and you would have to create a new RemoteEvent in ReplicatedStorage like this:

local event = Instance.new("RemoteEvent", game.ReplicatedStorage)
event.Name = "event"

Now you will send the message to the server that you want to change the character's health by putting event:FireServer(character). This will tell the server to do something and carry over the character data with it.

You now need to create a server script, preferably in ServerScriptService, named whatever you want for this. The server script has to receive the message using the OnClientEvent event.

local event = game.ReplicatedStorage:WaitForChild("event")

event.OnClientEvent:connect(function(character)
    character.Humanoid.Health = --whatever value you want
end)

If you need more info on RemoteEvents click here.

Hope this helped, good luck!

Ad

Answer this question