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 find the players character in Workplace and the player in Players for a onTouch part?

I have a part and I want it when a player touches It, It gives them a tool and takes away their health. So I need access to their character in Workspace defined as a variable and to their Backpack.

function Touch(player)
	local player = game.Players:FindFirstChild("player")
	local character = game.Workspace:FindFirstChild("player")
	game.ServerStorage.Tool:Clone().Parent = player.Backpack
	character.Health = character.Health - 50
end

script.Parent.Touched:connect(Touch)

1 answer

✓ Accepted answer
function Touch(part)
	local character = part.Parent
	if game.Players:GetPlayerFromCharacter(character) then
		local player = game.Players:FindFirstChild(character.Name)
		game.ServerStorage.Tool:Clone().Parent = player.Backpack
		character.Humanoid.Health = character.Humanoid.Health - 50
	end
end

script.Parent.Touched:connect(Touch) -- Guessing script in part

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