I want my touch event to fire once but its firing a lot of times even though I stop it
Server:
local intermissiontime = 5
local ball = game.Workspace.Ball
local basespeed = 20
local playerleft
local serverevent
local playercount = 0
game.Players.PlayerAdded:Connect(function()
local count = 0
for i,v in pairs(game.Players:GetChildren()) do
count+= 1
end
if count == 2 then
playercount = 0
start(playercount)
print("START")
else
Text("WAITING FOR PLAYERS")
end
end)
function start()
playerleft = game.Players.PlayerRemoving:Connect(function()
playercount-=1
end)
local timer = intermissiontime
repeat
Text(timer)
task.wait(1)
timer-=1
until timer == 0
for i,v in pairs(game.Players:GetChildren()) do
playercount+= 1
local value = Instance.new("BoolValue")
value.Parent = game.Workspace:FindFirstChild(v.Name)
end
local speed = basespeed
Text("Game Started")
local currentplayer = PickRandom(speed)
serverevent = game.ReplicatedStorage.Ball.OnServerEvent:Connect(function(plr,died)
print(died)
if died == true and playercount > 1 then
currentplayer = PickRandom(speed,currentplayer)
playercount -= 1
game.Workspace.kill:Play()
game.Workspace:FindFirstChild(currentplayer.Name):FindFirstChild("Humanoid").Health-= 100
elseif playercount > 1 then
speed*=1.1
currentplayer = PickRandom(speed,currentplayer)
end
if playercount <= 1 then
endgame()
end
end)
end
function endgame()
playerleft:Disconnect()
serverevent:Disconnect()
ball:SetNetworkOwner(nil)
ball.Position = game.Workspace.BallSpawn.Position
ball.BodyVelocity.Velocity = Vector3.new(0,0,0)
print(ball.BodyVelocity.Velocity)
playercount = 0
for i,v in pairs(game.Players:GetChildren()) do
if v:FindFirstChild("Value") then
v:FindFirstChild("Value"):Destroy()
end
end
start()
end
function Text(text)
for i,v in pairs(game.Players:GetChildren()) do
v:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextLabel").Text = text
end
end
function PickRandom(speed,prevplr)
print("PICK RANDOM FUNCTION RAN")
local pickedPlayer
repeat
local players = game:GetService("Players"):GetPlayers()
pickedPlayer = players[math.random(1, #players)]
until pickedPlayer.Name ~= prevplr and game.Workspace:FindFirstChild(pickedPlayer.Name):FindFirstChild("Value")
ball:SetNetworkOwner(pickedPlayer)
game.ReplicatedStorage.Ball:FireClient(pickedPlayer,speed)
return pickedPlayer
end
game.ReplicatedStorage.Effects.OnServerEvent:Connect(function(plr)
local handle = game.Workspace:FindFirstChild(plr.Name):FindFirstChild("Tool").Handle
handle.hit:Play()
for i,v in pairs(handle.HitEffect:GetChildren()) do
v:Emit(1)
end
end)
Client:
local connection
local db = false
game.ReplicatedStorage.Ball.OnClientEvent:Connect(function(speed)
local char = game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
local ball = game.Workspace.Ball
ball.Color = Color3.new(1, 0, 0)
connection = ball.Touched:Connect(function(hit)
if hit.Parent.Name == char.Name then
if db == false then
db = true
if char:FindFirstChild("Tool") and char:FindFirstChild("Tool").DB.Value == true then
char.Humanoid:LoadAnimation(script.Slash):Play()
game.ReplicatedStorage.Effects:FireServer()
game.ReplicatedStorage.Ball:FireServer(false)
print(db)
else
game.ReplicatedStorage.Ball:FireServer(true)
end
end
end
end)
repeat
task.wait()
game.Workspace.Ball.BodyVelocity.Velocity = ((char.HumanoidRootPart.Position-game.Workspace.Ball.Position)).Unit*speed
until db == true
connection:Disconnect()
print("RAN")
db = false
ball.Color = Color3.new(99, 95, 98)
end)