I believe that is what I have done, inside the death function, it does the LoadCharacter on the character that died (player1 or player2)
Can you resend your script with the changes? I probably missed something.
The way I would do it, would be to keep the state on the Player object, with Attributes.
Check the Attributes when a character spawns in
So if a character is flagged with an attribute to spawn out of the arena, (set then when the player is killed for the 5th time)
then when the player is added to the game (respawn after death) check for that flag (the attribute saying to spawn out of arena)
if its found, set it to nil, and spawn the character outside of the arena.
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2
local team1 = workspace.Arena1Team1
local team2 = workspace.Arena1Team2
local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local function startEvent()
local plr1CurrentKills = 0
local plr2CurrentKills = 0
local SwordGiver = game.ServerStorage.Sword
local plr1 = standingOnArena1Player1
local plr2 = standingOnArena1Player2
local Sword1 = SwordGiver:Clone()
local char1 = plr1.Character
local Sword2 = SwordGiver:Clone()
local char2 = plr2.Character
local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
local character1Loaded = false
local character2Loaded = false
print("Variables working fine")
if char1 or char2 then
local humanoid1 = char1:FindFirstChild("Humanoid")
local humanoid2 = char2:FindFirstChild("Humanoid")
if humanoid1 or humanoid2 then
Sword1.Parent = plr1.Backpack
humanoid1:EquipTool(Sword1)
Sword2.Parent = plr2.Backpack
humanoid2:EquipTool(Sword2)
print("Approved that i'm humanoid")
while plr1CurrentKills <= 5 do
wait(1)
char1.Humanoid.Died:Connect(function(tag)
local player1 = game.Players:GetPlayerFromCharacter(char1)
player1:LoadCharacter()
char1 = player1.Character
plr2CurrentKills = plr2CurrentKills + 1
char2.Humanoid.Health = 100
game.Players.player1:LoadCharacter()
character1Loaded = true
if character1Loaded then
Sword1.Parent = plr1.Backpack
humanoid1:EquipTool(Sword1)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
character1Loaded = false
end
end)
char2.Humanoid.Died:Connect(function(tag)
local player2 = game.Players:GetPlayerFromCharacter(char2)
player2:LoadCharacter()
char2 = player2.Character
plr1CurrentKills = plr1CurrentKills + 1
char1.Humanoid.Health = 100
game.Players.player2:LoadCharacter()
character2Loaded = true
if character2Loaded then
Sword2.Parent = plr2.Backpack
humanoid2:EquipTool(Sword2)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
character2Loaded = false
end
end)
wait(1)
end
end
end
end
RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
--Hide playbutton gui
standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
--Teleports
workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
local gameRunning = true
part1.Transparency = 1
part1.CanCollide = false
part2.Transparency = 1
part2.CanCollide = false
startEvent() -- triggers function
end
end)
-----------------------------------------------------
-- PART1 Events --
-----------------------------------------------------
-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]
part1.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied
part1.BrickColor = BrickColor.new("Teal") -- Set color
standingOnArena1Player1 = Player
end
end
end)
part1.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
if standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
end
part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnArena1Player1 = nil
if standingOnArena1Player2 ~= nil then
if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
-----------------------------------------------------
-- PART2 Events --
-----------------------------------------------------
part2.Touched:Connect(function(TouchedPart) -- Touched
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied
part2.BrickColor = BrickColor.new("Teal") -- Set color
standingOnArena1Player2 = Player
end
end
end)
part2.TouchEnded:Connect(function(TouchedPart)
local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)
if Player then -- Check if it found character
if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
end
part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
standingOnArena1Player2 = nil
if standingOnArena1Player1 ~= nil then
if standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
end
end
end
end
end)
while task.wait(0) do
if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
end
end
Thank you very much for the response, unfortunatly that is all above my skill level, I haven’t learnt about attributes yet
They are the most simple of things, can be set in code or studio.
They come in pretty handy for keeping track of small bits of data, such as states, or counters.
When you find the time, you should check them out.
The lines game.Players.player1:LoadCharacter()
and game.Players.player2:LoadCharacter()
are probably throwing errors and you don’t need them anyway since you already called LoadCharacter
, so you can remove those. Do that and try it again.
I have done that, and it was working for a while but now, whoever dies doesn’t teleport and it’s almost like as soon as their character is loaded (not in the arena) they die straight after. It’s weird. The player that doesn’t die however gets teleported perfectly fine. (I reassigned the humanoid1/2 value as I was having problems with the players receiving a sword after they died)
Here is my current script:
local function startEvent()
local plr1CurrentKills = 0
local plr2CurrentKills = 0
local SwordGiver = game.ServerStorage.Sword
local plr1 = standingOnArena1Player1
local plr2 = standingOnArena1Player2
local Sword1 = SwordGiver:Clone()
local char1 = plr1.Character
local Sword2 = SwordGiver:Clone()
local char2 = plr2.Character
local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
local character1Loaded = false
local character2Loaded = false
print("Variables working fine")
if char1 or char2 then
local humanoid1 = char1:FindFirstChild("Humanoid")
local humanoid2 = char2:FindFirstChild("Humanoid")
if humanoid1 or humanoid2 then
Sword1.Parent = plr1.Backpack
humanoid1:EquipTool(Sword1)
Sword2.Parent = plr2.Backpack
humanoid2:EquipTool(Sword2)
print("Approved that i'm humanoid")
while plr1CurrentKills and plr2CurrentKills <= 5 do
wait(1)
char1.Humanoid.Died:Connect(function(tag)
local player1 = game.Players:GetPlayerFromCharacter(char1)
wait(2)
player1:LoadCharacter()
char1 = player1.Character
local humanoid1 = char1.Humanoid
plr2CurrentKills = plr2CurrentKills + 1
char2.Humanoid.Health = 100
Sword1.Parent = player1.Backpack
humanoid1:EquipTool(Sword1)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
end)
char2.Humanoid.Died:Connect(function(tag)
local player2 = game.Players:GetPlayerFromCharacter(char2)
wait(2)
player2:LoadCharacter()
char2 = player2.Character
local humanoid2 = char2.Humanoid
plr1CurrentKills = plr1CurrentKills + 1
char1.Humanoid.Health = 100
Sword2.Parent = player2.Backpack
humanoid2:EquipTool(Sword2)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
end)
wait(1)
end
part1.Transparency = 0
part1.CanCollide = true
part2.Transparency = 0
part2.CanCollide = true
end
end
end
Anything in output?
jdndisbsuabsu30
Hi, the only error message I received is ’ The Parent property of Sword is locked, current parent: NULL, new parent Backpack’ but I noticed that the dead player does teleport but it’s almost like they teleport and then teleport straight to spawn after, its weird. Another thing I noticed is it seemed to run the death script 4 times, as it kept saying ‘Stack Begin - Studio
Script ‘ServerScriptService.Arena1Script’, Line 71 - Studio - Arena1Script:71
Stack End - Studio’ on line 71, which is when the player receives the sword after dying. It might be something to do with, its running the death script over and over until one of the players kills is >= 5 and therefore the player doesn’t get teleported, but i’m not really sure what’s going on. Got any ideas? Thanks.
I think the issue for teleporting might be because if you try and teleport a character right after spawning, it will teleport before being sent to spawn, so then it will get sent to spawn right after. What you should do instead is wait until the player is fully loaded. I think CharacterAppearanceLoaded should work.
player.CharacterAppearanceLoaded:Wait()
--tp character here
Edit: I think the reason why it keeps teleporting multiple times is because you do a while loop and each loop connects the died event to the same function, doing the same thing over and over again. Instead, you need to just connect the function once. I recommend each checking the kills each death, so you don’t have to do a while loop all the time and it will instantly know when it’s done. Then, you can disconnect the connection inside the connected part. Here is an example:
local connections = {}
local function disconnectConnections(kills)
if kills > 5 then
for i, v in ipairs(connections) do
v:Disconnect()
end
end
end
connections[1] = char1.Humanoid.Died:Connect(function(tag)
local player1 = game.Players:GetPlayerFromCharacter(char1)
wait(2)
player1:LoadCharacter()
char1 = player1.Character
local humanoid1 = char1.Humanoid
plr2CurrentKills = plr2CurrentKills + 1
disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
char2.Humanoid.Health = 100
Sword1.Parent = player1.Backpack
humanoid1:EquipTool(Sword1)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
end)
connections[2] = char2.Humanoid.Died:Connect(function(tag)
local player2 = game.Players:GetPlayerFromCharacter(char2)
wait(2)
player2:LoadCharacter()
char2 = player2.Character
local humanoid2 = char2.Humanoid
plr1CurrentKills = plr1CurrentKills + 1
disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5
char1.Humanoid.Health = 100
Sword2.Parent = player2.Backpack
humanoid2:EquipTool(Sword2)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
end)
Thank you very much for the response, it is working much better now, both players are teleporting, but for some reason the player that dies still isn’t receiving a sword. Do you have any idea why that is? I’ll check the output and let you know if it says anything. Thanks!
Edit: No error messages
try print the sword’s parent and tell me what it says
By the way, I just tested it a bit more and I have a feeling it still thinks one of the players has reached 5 kills because part1.Transparency = 0 part1.CanCollide = true part2.Transparency = 0 part2.CanCollide = true
This happens straight after i kill the player and this is outside the while loop. Also, the dead player teleports after the first time I kill them, but if I kill them again they don’t teleport
I don’t think you should have a while loop. What you’re doing is connecting the died event to the function multiple times, so this function will run multiple times. Additionally, each time the while loop runs, the mentioned lines run too. I don’t think you understand that when you connect an event to a function, the script will just carry on and whenever the event is fired the function will run.
So if I just remove the while loop will it still work? Sorry i’m very new to scripting so struggle to understand
I’m not really sure how to explain it. Here’s an updated version of the code sent in one of my previous replies. Replace the while loop with this.
local connections = {}
local function disconnectConnections(kills)
if kills > 5 then
for i, v in ipairs(connections) do
v:Disconnect()
end
-- do everything you need to do here once someone reaches over 5 kills
end
end
connections[1] = char1.Humanoid.Died:Connect(function(tag)
local player1 = game.Players:GetPlayerFromCharacter(char1)
wait(2)
player1:LoadCharacter()
char1 = player1.Character
local humanoid1 = char1.Humanoid
plr2CurrentKills = plr2CurrentKills + 1
disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
char2.Humanoid.Health = 100
Sword1.Parent = player1.Backpack
humanoid1:EquipTool(Sword1)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
end)
connections[2] = char2.Humanoid.Died:Connect(function(tag)
local player2 = game.Players:GetPlayerFromCharacter(char2)
wait(2)
player2:LoadCharacter()
char2 = player2.Character
local humanoid2 = char2.Humanoid
plr1CurrentKills = plr1CurrentKills + 1
disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5
char1.Humanoid.Health = 100
Sword2.Parent = player2.Backpack
humanoid2:EquipTool(Sword2)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
end)
I’ll try explain it the best I can. Events are a thing which will do certain things once fired. .died, for example, is an event that is fired whenever the character dies. You can connect them to functions, using :Connect(), and it will run the function every time they die.
Code example:
local function onDeath(char) -- the name does not matter by the way
print(char.Name.." has died!")
end
player.Died:Connect(onDeath)
Every time the player dies, it will print the player’s name has died. What you’re doing, however, is instead of referencing a function, you’re putting the function itself in there. Here is the same code except using the way you’re doing it:
player.Died:Connect(function(char)
print(char.Name.." has died!")
end) -- this closing bracket is important as the function is just the argument to the :Connect() function. you will have an unclosed set of parentheses if you don't do this.
The way this works is you define the function inside of the parentheses itself. Just imagine it as one single argument inside of the parentheses, that being the function.
This will always run the function whenever they die. It will never stop running, unless you disconnect the connection. How do you do this, you may ask. Well first, you set the connection as a variable. Then you can call the :Disconnect() function on it. Here’s an example:
local connection -- must be defined outside of the connection otherwise it cannot be disconnected inside of the function
connection = player.Died:Connect(function(char)
print(char.Name.." has died!")
connection:Disconnect() -- the function will no longer run when the character dies
end)
The connection can be stored in other data structures, however. I usually store it in an array, so then when I want to disconnect every connection I can loop through them all. Here is an example:
local connections = {} -- define it as a table
local function died(char)
print(char.Name.." has died!")
-- disconnecting all connections
for i, v in ipairs do -- loops through all values in the array, with i being the index and v being the value
v:Disconnect() -- disconnects the connection
end
end
connections[1] = player1.Died:Connect(died)
connections[2] = player2.Died:Connect(died)
I hope this made sense. If it didn’t, feel free to ask any questions.
Thank you so much I really appreciate your help, it makes a lot more sense now, the script is working a lot better now, as the dead player receives a sword after they die. However if I kill the player again, they don’t teleport, I’m not sure if I copied the script wrong or something.
Here is the script:
local function startEvent()
local plr1CurrentKills = 0
local plr2CurrentKills = 0
local SwordGiver = game.ServerStorage.Sword
local plr1 = standingOnArena1Player1
local plr2 = standingOnArena1Player2
local Sword1 = SwordGiver:Clone()
local char1 = plr1.Character
local Sword2 = SwordGiver:Clone()
local char2 = plr2.Character
local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
local character1Loaded = false
local character2Loaded = false
print("Variables working fine")
if char1 or char2 then
local humanoid1 = char1:FindFirstChild("Humanoid")
local humanoid2 = char2:FindFirstChild("Humanoid")
if humanoid1 or humanoid2 then
Sword1.Parent = plr1.Backpack
humanoid1:EquipTool(Sword1)
Sword2.Parent = plr2.Backpack
humanoid2:EquipTool(Sword2)
print("Approved that i'm humanoid")
local connections = {}
local function disconnectConnections(kills)
if plr1CurrentKills or plr2CurrentKills > 5 then
for i, v in ipairs(connections) do
v:Disconnect()
end
plr1CurrentKills = 0
plr2CurrentKills = 0
part1.Transparency = 0
part1.CanCollide = true
part2.Transparency = 0
part2.CanCollide = true
end
end
connections[1] = char1.Humanoid.Died:Connect(function(tag)
local player1 = game.Players:GetPlayerFromCharacter(char1)
wait(2)
player1:LoadCharacter()
char1 = player1.Character
local humanoid1 = char1.Humanoid
plr2CurrentKills = plr2CurrentKills + 1
disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
char2.Humanoid.Health = 100
Sword1.Parent = player1.Backpack
humanoid1:EquipTool(Sword1)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
end)
connections[2] = char2.Humanoid.Died:Connect(function(tag)
local player2 = game.Players:GetPlayerFromCharacter(char2)
wait(2)
player2:LoadCharacter()
char2 = player2.Character
local humanoid2 = char2.Humanoid
plr1CurrentKills = plr1CurrentKills + 1
disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5
char1.Humanoid.Health = 100
Sword2.Parent = player2.Backpack
humanoid2:EquipTool(Sword2)
char1.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player1Spawn.Position
)
char2.HumanoidRootPart.CFrame = CFrame.new(
Arena1Player2Spawn.Position
)
end)
end
end
end
Edit: I change the ‘kills’ value with ‘plr1CurrentKills or plr2CurrentKills’ as I have a leaderboard value with ‘Kills’ which I think would have messed with the script. But I have just realised you have put ‘Kills’ in the parenthesis too so it might of messed up the script as the value in the parenthesis are different to the if statement below
Ah sorry I didn’t read the script properly I’ll change it back I just noticed you assigned plr1/2CurrentKills to ‘kills’
Ok so I changed it back and I tested it. After one player kills the other, they both get teleported and regiven swords, which is perfect. But if they kill eachother again, neither player gets teleported or gets swords.