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

How do I refer to the player that touches something?

Asked by 5 years ago

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

0
GetPlayerFromCharacter(), also, don't use Touched on the client. Use it on the server. DeceptiveCaster 3761 — 5y

3 answers

Log in to vote
0
Answered by
U_srname 152
5 years ago
Edited 5 years ago

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
Im so sorry I meant to say the object that is touched. Could you help with that? 50ShadesofLamps 5 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

0
This script of yours looks incredibly copyrighted mind crediting the person who you got it fro if it is. TheEpicNoob1111 15 — 5y
Log in to vote
0
Answered by 5 years ago

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)
0
This isn't correct and won't work U_srname 152 — 5y

Answer this question