Hi! So I am making a zombie system where a zombie follows a player and attacks it with a hitbox welded to the torso. So far the following script I got is working fine however if I make more than 1 zombie and they touch each other, they start damaging each other and I want only the player to be damaged. Someone told me I can use GetPlayerFromCharcter
but I can't really understand how to use that since I've never heard of it. Here is the script that I'm trying to fix with GetPlayerFromCharacter. The script is attached to the hitbox whenever the player touches the hitbox:
function touch(hit) hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -3 end script.Parent.Touched:connect(touch)
If you know how I can use GetPlayerFromCharacter
to only let the zombies damage the players and not themselves, please tell me!
[Re-edited by JessseSong]
Fixed Grammar and code block!
Reference(s):
Its possible.
function touch(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -3 end end script.Parent.Touched:connect(touch)
Basically, if its not a players character, GetPlayerFromCharacter returns nil.
:GetPlayerFromCharacter is self explanatory. There are effectively two versions of a player on Roblox. There is the 'player' which is stored in the Players section of the explorer it contains metadata about the player, GUI's, stored tools, etc. The character on the other hand is stored in the workspace and it is the physical body of the player. So to use GetPlayerFromCharacter() you get to first access the Players section.
local Players = game.Players local PlarChar = Players:GetPlayerFromCharacter("PlayersNameHere") print(PlrChar) -- Will print the Character of the player