Hello everyone! Today I was making this duels system for my game. Everything works except for the walls destroying. I know the issue is that the remote event is firing twice on the server. But the issue is I don’t know how to fix it. I’ve tried destroying it client side but still. Nothing works! This is my local script which fires the remote event(it fires it twice because theres 2 players.)
while wait() do
if VotesValue.Value == 2 then
Button.Visible = false
game.ReplicatedStorage.RemoteEvents.StartRoundEvent:FireServer(VotesValue.Value)
local FindMap = workspace:FindFirstChild(Player.GameStats.MapChosen.Value)
break
end
This is the line in the server script that’s supposed to destroy the walls.
FindChosenMap.Walls:Destroy()
This is FindChosenMap is equal to.
local FindChosenMap = workspace:FindFirstChild(MapChosen.Value)
This is the error the console gets:
If you’d like the full script then say below! Any help is apreciated!
Instead of an infinite loop, you should connect to an event, like perhaps when the Votes value changes. That way you can be sure to only Fire the remote once for that player. You can also set up a debounce on the server so that if the remote is fired multiple times, as in once for each player, you can make sure it only runs once at a time. A debounce is simply checking if a variable is true or false at any given time. if its false, you can set it to true, wait some time, then set it to false again.
Also the reason for the error is because you aren’t checking if Walls exist before attempting to reference/destroy it. You are only checking if the Map exists.
Didn’t work. I think the script firing twice has something to do with it. Because sometimes the game glitches and doesn’t fire it for the other player. THATS when it works! I think i’ll have to make 2 scripts in order to fix this. I’ll keep you all updated.
local IsPlaying = true
local RemovedWalls = game.ReplicatedStorage:WaitForChild("Values"):WaitForChild("RemovedWalls")
game.ReplicatedStorage.RemoteEvents.StartRoundEvent.OnServerEvent:Connect(function(Player, Votes)
local StopWatch = Player.GameStats:WaitForChild("StopWatch")
local MapChosen = Player.GameStats.MapChosen
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local FindChosenMap = workspace:FindFirstChild(MapChosen.Value)
if FindChosenMap then
if game.ReplicatedStorage.Values.VotesValue.Value >= 2 then
if FindChosenMap:FindFirstChild("Walls") then
FindChosenMap:WaitForChild("Walls"):Destroy()
end
game.ReplicatedStorage.RemoteEvents.StartCountDownEvent:FireAllClients("DONTOTNT")
Humanoid.WalkSpeed = 0
Humanoid.JumpPower = 0
HRP.Position = FindChosenMap.SpawnLocation.Position
FindChosenMap.EndPart.Touched:Connect(function()
IsPlaying = false
end)
wait(3)
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
local CurrentValue = 0
while IsPlaying == true do
CurrentValue += task.wait()
StopWatch.Value = CurrentValue - CurrentValue % 0.001
end
end
else
print("Map not found.")
end
end)
local IsPlaying = true
local RemovedWalls = game.ReplicatedStorage:WaitForChild("Values"):WaitForChild("RemovedWalls")
game.ReplicatedStorage.RemoteEvents.StartRoundEvent.OnServerEvent:Connect(function(Player, Votes)
local StopWatch = Player.GameStats:WaitForChild("StopWatch")
local MapChosen = Player.GameStats.MapChosen
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local FindChosenMap = workspace:FindFirstChild(MapChosen.Value)
print("MAP PICKED: "..FindChosenMap)
if FindChosenMap then
if game.ReplicatedStorage.Values.VotesValue.Value >= 2 then
if FindChosenMap:WaitForChild("Walls") then
FindChosenMap:WaitForChild("Walls"):Destroy()
end
game.ReplicatedStorage.RemoteEvents.StartCountDownEvent:FireAllClients("DONTOTNT")
Humanoid.WalkSpeed = 0
Humanoid.JumpPower = 0
HRP.Position = FindChosenMap.SpawnLocation.Position
FindChosenMap.EndPart.Touched:Connect(function()
IsPlaying = false
end)
wait(3)
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
local CurrentValue = 0
while IsPlaying == true do
CurrentValue += task.wait()
StopWatch.Value = CurrentValue - CurrentValue % 0.001
end
end
else
print("Map not found.")
end
end)
dont do this. if you are checking if something exists, FindFirstChild is the one to use. Also if it does find it, you do not need to then WaitForChild to destroy it because it found it which means it exists.
I hope you are not using this as a way to check if multple players add 1 to this, because the server will not see changes made from the client. You will have to have those clients FireServer to then have the server make those changes.
VotesValue.Changed:Connect(function()
if VotesValue.Value == 2 then
Button.Visible = false
game.ReplicatedStorage.RemoteEvents.StartRoundEvent:FireServer(VotesValue.Value)
local FindMap = workspace:WaitForChild(Player.GameStats.MapChosen.Value)
end
end)
Sorry for the late responce. This is the full local script:
local Button = script.Parent
local VotesValue = game.ReplicatedStorage.Values.VotesValue
local DontBreak = "kiolsdjifds;jofsdf"
local Player = game.Players.LocalPlayer
local HasVoted = Player.GameStats.HasVoted
local RemoteEvent = game.ReplicatedStorage.RemoteEvents.CastPlayerVoteEvent
Button.MouseButton1Click:Connect(function()
if HasVoted.Value == false then
HasVoted.Value = true
RemoteEvent:FireServer(Button)
else
HasVoted.Value = false
RemoteEvent:FireServer(Button)
end
end)
VotesValue.Changed:Connect(function()
Button.Text = "Start(" .. VotesValue.Value .. "/2)"
if VotesValue.Value == 2 then
Button.Visible = false
game.ReplicatedStorage.RemoteEvents.StartRoundEvent:FireServer(VotesValue.Value)
local FindMap = workspace:FindFirstChild(Player.GameStats.MapChosen.Value)
end
end)