Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I refer to the player that touches something?

I tried

local function epicvbuck(objecttouched ,toucher)
	print("whateva")
	objecttouched.Anchored = false
end


game.Players.LocalPlayer.Character.Humanoid.Touched:Connect()(epicvbuck)
	


this but in output whenever I touch something it reads "attempt to read a nil value"

it only prints "whateva" one time then stops I would also like to refer to the whole player not a single bodypart

I want it whenevera player touches any brick itll unanchor and fall i dont want to have to copy and paste a touched script into every single part in my game thanks in advance

1 comment

0

GetPlayerFromCharacter(), also, don't use Touched on the client. Use it on the server.

3 answers

If you want Touched events, you have to check if the part you want touched is touching something. Then, you have to check if it's a humanoid, indicating it is a player.

This is what your code should look like:

script.Parent.Touched:Connect(function(hit) -- script.Parent is objecttouched, hit is toucher
	local humanoid = hit.Parent:FindFirstChild('Humanoid') -- check if humanoid

	if humanoid then -- if it is humanoid then do this
		print('whateva')
		script.Parent.Anchored = false
	end
end)

The hit is how you reference the object when using a Touched event.

Hope I helped!

1 comment

1

Im so sorry I meant to say the object that is touched. Could you help with that?

Here:

local Part = game.Workspace.Part   --Getting the Part

Part.Touched:Connect(function(hit)  --Detecting if the Part is touched and running the function: hit


local Humanoid = hit.Parent:FindFirstChild("Humanoid")  --This gets the humanoid

if Humanoid then --This detects if the thing that hit it is a Humanoid

Humanoid.Health = 0 --Kills the player on touch

end

end)

This worked for me, I hope it works for you.

1 comment

0

This script of yours looks incredibly copyrighted mind crediting the person who you got it fro if it is.

This should work havent tried it but hope it does it looks like you are a beginner scripter by the way you set up your script.

local function epicvbuck(Part)
    print("whateva")
    Part.Anchored = false -- Part Not Anchored
end

game.Players.LocalPlayer.Character.Humanoid.Touched:Connect(epicvbuck)

1 comment

0

This isn't correct and won't work

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