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

How to use GetPlayerFromCharacter on a damage script?

Asked by 3 years ago
Edited by JesseSong 3 years ago

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!

  • Date: 14/03/2021
  • Time 20:23 (GMT)

Reference(s):

Learn how to post a good Q/A here

0
What people mostly use GetPlayerFromCharacter is for seeing if whatever touched the part is a player KixWater 126 — 3y
0
Basically touch events etc. JesseSong 3916 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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.

1
Thanks! Bluejay20000 -11 — 3y
1
You're welcome. Rampage_Doge 131 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

: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 

Answer this question