I want to make a hot potato minigame for a game I’m making, however for some reason I can only pass the potato once, afterwards it just stops working forever.
In output it shows if the potato is to be passed afterwards, the print in the Touched event reports that when the person with the potato tries to pass it, the Touched event reports it as if they touched themselves and doesn’t pass the bomb.
Any idea why this is going on? Script:
local plr = game:GetService("Players"):FindFirstChild(name)
local bomber = plr.Character
local success, err = pcall(function()
local db = false
local count = 30
local oldhead
if not bomber.Head:FindFirstChild("Count") then
local gui = script.Count:Clone()
gui.Parent = bomber.Head
connection = bomber:FindFirstChild("Torso").Touched:Connect(function(hit)
print(bomber.Name .. " hit by " .. hit.Parent.Name)
local success, err = pcall(function()
--if hit.Parent.Head:FindFirstChild(gui.Name) then
if bomber.Head:FindFirstChild("Count") then
print("test db")
if db == false then
db = true
print("giving")
oldhead = bomber.Head
if hit.Parent:IsA("Hat") then
bomber = hit.Parent.Parent
elseif hit.Parent:IsA("Accessory") then
bomber = hit.Parent.Parent
else
bomber = hit.Parent
end
gui.Parent = bomber.Head
print(oldhead.Parent.Name .. " gave " .. hit.Parent.Name .. " the bomb!")
wait(0.5)
db = false
print("db off")
connection:Disconnect()
end
end
--end
end)
end)
while count > -1 do
wait(1)
count = count - 1
gui.TextLabel.Text = tostring(count)
if count == 0 then
print("BOOM UR DEAD")
local victim = gui.Parent.Parent
if victim then
local boom = Instance.new("Explosion")
boom.BlastPressure = 0
boom.BlastRadius = 0
boom.DestroyJointRadiusPercent = 0
boom.ExplosionType = Enum.ExplosionType.NoCraters
boom.Parent = victim:FindFirstChild("Torso")
boom.Position = victim:FindFirstChild("Torso").Position
wait(0.2)
local hum = victim:FindFirstChild("Humanoid")
hum.Health = 0
gui:Destroy()
end
end
end
end
end)
if err then
warn("ERROR: " .. err)
end
My theory is that the Touched event still listens for collisions on the old player, but I have no idea how to do it any other way
disconnect .Touched when passing on the hot potato
Already did that, or is it in the wrong place?
maybe dont yield the disconnection.
Nothing, I think I’ll just use the hot potato gear
You Forgot To Connect Touched Again At The New Player
The current idea of yours is that when a player with a hot potato touches another player, the other player will execute the entire potato thing again. However, as this is not in a loop, it is expected that you can only pass it for once, as the Touch thing is only being executed once at the first player.
I assume that this script goes on the server.
Maybe you can try a different approach.
First, you make a variable to hold the player who has the potato, and another variable for time.
Second, you write a function to pass the potato to a new player.
In which, set the variable to the new player and reset the time, when the new player touches another player, execute the function again on the other player.
Third, you make a while loop to deduct the time when player ~= nil. It breaks when time reaches 0. Then you can do stuff to the player who has the potato by refering to the player variable.
This can probably simplify your code.
Same issue, am I doing this correct?
local success, err = pcall(function()
local origin = game:GetService("Players"):FindFirstChild(name).Character
local bomber = origin
local db = false
local timer = 30
local hasHappened = false
if not origin.Head:FindFirstChild("Count") then
local gui = script.Count:Clone()
gui.Parent = origin.Head
local function pass(gifted)
if db == false then
db = true
print("Attempting to pass")
if gifted:FindFirstChild("Head") then
print("Head found")
local head = gifted.Head
print(bomber.Name .. " is bomber, " .. gifted.Name .. " is gifted")
if bomber.Head:FindFirstChild("Count") then
print("Bomber has bomb")
if not head:FindFirstChild("Count") then
print("Victim doesn't have bomb")
print("Giving bomb from " .. bomber.Name .. " to " .. gifted.Name)
bomber = gifted
gui.Parent = head
print("Done, listening for touch")
connection2 = bomber.Torso.Touched:connect(function(hit)
if hit.Parent:IsA("Hat") or hit.Parent:IsA("Accessory") then
local victim = hit.Parent.Parent
pass(victim)
connection2:Disconnect()
else
local victim = hit.Parent
pass(victim)
connection2:Disconnect()
end
end)
end
end
end
wait(0.5)
db = false
end
end
connection = bomber.Torso.Touched:connect(function(hit)
if hasHappened == false then
if hit.Parent:IsA("Hat") or hit.Parent:IsA("Accessory") then
local victim = hit.Parent.Parent
pass(victim)
print("First time pass")
hasHappened = true
connection:Disconnect()
else
local victim = hit.Parent
pass(victim)
print("First time pass")
hasHappened = true
connection:Disconnect()
end
end
end)
while timer > -1 do
wait(1)
timer = timer - 1
gui.TextLabel.Text = tostring(timer)
if timer == 0 then
print("BOOM UR DEAD")
if bomber then
local boom = Instance.new("Explosion")
boom.BlastPressure = 0
boom.BlastRadius = 0
boom.DestroyJointRadiusPercent = 0
boom.ExplosionType = Enum.ExplosionType.NoCraters
boom.Parent = bomber:FindFirstChild("Torso")
boom.Position = bomber:FindFirstChild("Torso").Position
wait(0.2)
local hum = bomber:FindFirstChild("Humanoid")
hum.Health = 0
gui:Destroy()
end
end
end
end
end)