You can write your topic however you want, but you need to answer these questions:
I want to decrease a value when a person leaves and their team is red.
What is the issue? Include screenshots / videos if possible!
The remote event not working.
-- in workspace
local RedAmount = game.ReplicatedStorage.TeamLimits.RedAmount
local BlueAmount = game.ReplicatedStorage.TeamLimits.BlueAmount
game.ReplicatedStorage.DeR.OnServerEvent:Connect(function()
wait()
RedAmount.Value = RedAmount.Value - 1
print("TeamRedAmount Decreased")
end)
-- this is in the startergui
game.Players.PlayerRemoving:connect(function(player)
if player.Name == game.Players.LocalPlayer.Name then
if game.Players.LocalPlayer.Team == game.Teams.Red then
wait(1)
game.ReplicatedStorage.DeR:FireServer()
end
end
end)
I have tried renaming and doing things like that and moving the events.
Firstly I advise you to move your workspace script to ServerScriptService.
Secondly, this line is completely useless and should be removed. You might also want to remove your wait() in the script that controls what happens when your RemoteEvent is fired.
It still isn’t clear to me what the error could be. May I please see what the output tells you?
Alright I am not sure why you are using a LocalScript in the StarterGui for this but it is much better to do everything in a single Server Script in ServerScriptService like so:
local RedAmount = game:GetService("ReplicatedStorage").TeamLimits.RedAmount
local BlueAmount = game:GetService("ReplicatedStorage").TeamLimits.BlueAmount
game:GetService("Players").PlayerRemoving:Connect(function(player)
if player.Team == game:GetService("Teams").Red then
RedAmount.Value = RedAmount.Value - 1
end
end)
In the StarerGui code which is triggered from the .PlayerRemoving event:
if game.Players.LocalPlayer.Team == game.Teams.Red then
wait(1)
game.ReplicatedStorage.DeR:FireServer()
end
When the player leaves the game, their window is closed and Roblox stops executing scripts, so effectively you are waiting for 1 second but then the game leaves so the code will never reach the :FireServer() line.
It is best practice to handle this sort of script on the server! Set up a script on the server somewhere that detects players leaving instead of the client doing it.