My script causes a lot of errors and lag on the ServerSide. Any tips?

I’ve been working on a game for some time now, and I’ve tried to implement a mechanism to prevent spawn killing. Any tips on how to improve it? It constantly give the error

Workspace.eTops.current.sandtops.Team Spawns.barrier.Script:3: attempt to index nil with "TeamColor"

script.Parent.Touched:Connect(function(otherPart)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
	if plr.TeamColor == game:GetService("Teams"):FindFirstChild("Blue Team").TeamColor then
		plr:LoadCharacter()
		game.ReplicatedStorage.eTops.ServerClient.ACNotify:FireClient(plr, 2)
	end
end)

This is my current script. ACNotify is a Remote Event and just sends a client notification if the player gets respawned

otherPart might not be the player, as Touched runs every time something touches the part (doesnt have to be the player)

script.Parent.Touched:Connect(function(otherPart)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
    if not plr then return end
    if plr.Team == game:GetService("Teams")["Blue Team"] then
        plr:LoadCharacter()
        game.ReplicatedStorage.eTops.ServerClient.ACNotify:FireClient(plr, 2)
    end
end)

Thank you, trying it right now.

yeah add a debounce

local debounces = {}

script.Parent.Touched:Connect(function(otherPart)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
    if not plr then return end
    if debounces[plr.UserId] then return end
    if plr.Team == game:GetService("Teams")["Blue Team"] then
        debounces[plr.UserId] = true
        plr:LoadCharacter()
        game.ReplicatedStorage.eTops.ServerClient.ACNotify:FireClient(plr, 2)

        task.delay(2, function() debounces[plr.UserId] = nil end)
    end
end)
1 Like

Thank you so much for your fast and good help, I appreciate you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.