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:
-- VARIABLES -- local blacklisted = false local playerService = game:GetService("Players") local player = playerService.LocalPlayer local gui = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui").HitDamagePartCount local valueText = gui.CountTextValue local valueInt = gui.CountValue local count = gui.Count local teleporter = workspace.Teleport local touch = workspace.Touch -- FUNCTION -- function DamageGuiChanger(hit) if blacklisted == false then local humRoot = hit.Parent:FindFirstChild("HumanoidRootPart") if hit == humRoot then if valueInt.Value < 4 then valueInt.Value = valueInt.Value + 1 print(hit) else humRoot.CFrame = CFrame.new(teleporter.Position.X, teleporter.Position.Y + 2, teleporter.Position.Z) wait() valueInt.Value = 0 end if valueInt.Value == 0 then count.TextColor3 = Color3.new(0, 0.666667, 0) elseif valueInt.Value == 1 then count.TextColor3 = Color3.new(0.333333, 0.666667, 0) elseif valueInt.Value == 2 then count.TextColor3 = Color3.new(0.666667, 0.666667, 0) elseif valueInt.Value == 3 then count.TextColor3 = Color3.new(1, 1, 0) elseif valueInt.Value == 4 then count.TextColor3 = Color3.new(1, 0.666667, 0) else count.TextColor3 = Color3.new(1, 0, 0) end end blacklisted = true wait(.5) blacklisted = false end end -- MAIN -- while wait() do valueText.Value = "5 of "..tostring(valueInt.Value).." hits to get teleported back!" count.Text = valueText.Value touch.Touched:Connect(DamageGuiChanger) end
You may look between line 19 and 46. Hopefully somebody can help me. G'Day.
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:
local part = script.Parent part.Touched:Connect(function() -- to add touch interest end) while wait() do local touchingParts = part:GetTouchingParts() -- gets all touching parts for _, touchingPart in ipairs(touchingParts) do local char = touchingPart.Parent local hum = char:FindFirstChild("Humanoid") -- if it's a character if hum and hum.Health > 0 then -- if humanoid is found hum.Health -= 150 -- kills the player end end end
If you want to add a cooldown between part touching you can use debounce:
local part = script.Parent part.Touched:Connect(function() -- to add touch interest end) local cooldown = false -- cooldown while wait() do if cooldown == false then -- if its off cooldown local touchingParts = part:GetTouchingParts() for _, touchingPart in ipairs(touchingParts) do local char = touchingPart.Parent local hum = char:FindFirstChild("Humanoid") if hum and hum.Health > 0 then -- if humanoid is found cooldown = true -- sets it on cooldown hum.Health -= 10 -- kills the player wait(10) -- cooldown time cooldown = false -- you can remove 2 line above if u want 1 time use end end end end