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

Why .Touch Event only runs after jumping on it?

Asked by 4 years ago

Hello, I want to script so if you touched a part five times, you'll get teleported back to the spawn point. It works fine, managed to fix how to update a gui and it wont loop while you stand on it, but it wont run everytime. There's no error given in output section. I've also looked for some solutions via Google, but no success. Here's the code:

01-- VARIABLES --
02local blacklisted = false
03 
04local playerService = game:GetService("Players")
05local player = playerService.LocalPlayer
06 
07local gui = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").HitDamagePartCount
08 
09local valueText = gui.CountTextValue
10local valueInt = gui.CountValue
11 
12local count = gui.Count
13 
14local teleporter = workspace.Teleport
15local touch = workspace.Touch
View all 55 lines...

You may look between line 19 and 46. Hopefully somebody can help me. G'Day.

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Hello!

Touched event only fires when the part begins to touch another part. I'd recommend you to use :GetTouchingParts() function to get any parts that are currently touching your parts

Also :GetTouchingParts() returns nil when the part has no TouchInterest, so you'll need to attach .Touched event

Example Code:

01local part = script.Parent
02 
03part.Touched:Connect(function()
04    -- to add touch interest
05end)
06 
07while wait() do
08    local touchingParts = part:GetTouchingParts() -- gets all touching parts
09 
10    for _, touchingPart in ipairs(touchingParts) do
11        local char = touchingPart.Parent
12        local hum = char:FindFirstChild("Humanoid") -- if it's a character
13 
14        if hum and hum.Health > 0 then -- if humanoid is found
15            hum.Health -= 150 -- kills the player
16        end
17    end
18end

If you want to add a cooldown between part touching you can use debounce:

01local part = script.Parent
02 
03part.Touched:Connect(function()
04    -- to add touch interest
05end)
06 
07local cooldown = false -- cooldown
08while wait() do
09    if cooldown == false then -- if its off cooldown
10        local touchingParts = part:GetTouchingParts()
11 
12        for _, touchingPart in ipairs(touchingParts) do
13            local char = touchingPart.Parent
14            local hum = char:FindFirstChild("Humanoid")
15 
View all 26 lines...
0
Thanks, I'll look if it works. If it does, I'll accept ur answer. R4nd0ml0l2 105 — 4y
Ad

Answer this question