I’m trying to make a function where when the players character head touches a special part, a gui pops up but first of the character part touch event isn’t firing. So I know there’s no problem with the function but the event isn’t triggering.
Is there something wrong with how my code is setup?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local Head = character:WaitForChild("Head")
local debounce
Head.Touched:Connect(function()
print("Touched")
end)
local function TouchEnded(part)
wait(0.1)
debounce = false
end
--character.Head.TouchEnded:Connect(TouchEnded)
Note: This is inside StarterGui inside a LocalScript.
1 Like
Side note, I would not recommend using TouchEnded
Try implementing print()
statements outside the function when the script starts to run?
I tried testing print outside the function to see if the script works which it does.
Do you mean not using the name “TouchEnded” for the function? Or do you mean not using TouchEnded entirely for character part event?
Edit: Both the Touched and TouchEnded events aren’t firing.
The TouchEnded
event is what I meant
Maybe try doing this?
local RepStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local DB = false
Head.Touched:Connect(function()
if not DB then
DB = true
print("This should fire?")
wait(1)
DB = false
end
end)
Oh I see what you mean… there isn’t anything wrong with the things thats inside the function but more like the input to trigger that function isn’t working.
I tried it out, it didn’t work.
Try putting the script inside StarterPlayerScripts
instead? Or maybe detecting with the Humanoid.Touched
Event?
local RepStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local DB = false
Humanoid.Touched:Connect(function(Part, BodyPart)
print(BodyPart)
if not DB then
DB = true
print("This should fire?")
wait(1)
DB = false
end
end)
Another alternate I suppose would be to check it on the server side, but I don’t know if it’d make any difference
1 Like
Apparently it wasn’t because the script wasn’t working but because the part I was touching had its property CanTouch set to false.
I thought that property only meant you can’t setup connect Touch events with that part. I misunderstood, CanTouch also determines whether this part will trigger other Touch events.
Where I found it: New Part Collision Property: CanTouch [ACTIVATED]
1 Like