Hi so basically I am creating a sword-fighting game with my friends to go in a game and I am trying to detect when 2 people stand on 2 parts they get teleported to a place and get auto-equipped with a sword. There is only 1 stand so only 2 people can play at once. This is my script so far, could someone help? Thank you xoxo
ALSO: When one of them dies, playing needs to go back to false and decider needs to go back to 0. That was one of the main issues.
local part1 = script.Parent.Part1 local part2 = script.Parent.Part2 local tp1 = script.Parent.Parent.Tp1 local tp2 = script.Parent.Parent.Tp2 local decider = 0 local playing = false part1.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local plr = workspace[hit.Parent.Name] --Access character from workspace. local player = game.Players:GetPlayerFromCharacter(plr) --Access (plr) decider = decider+1 end end) part2.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local plr = workspace[hit.Parent.Name] --Access character from workspace. local player = game.Players:GetPlayerFromCharacter(plr) --Access (plr) decider = decider+2 end end) if decider == 2 then end
buni xoxo
What you would need to do is store each player in two values when they step on the part, and remove them when they get off. And add a while loop to continuously check if both values are valid before teleporting...
local part1 = script.Parent.Part1 local part2 = script.Parent.Part2 local tp1 = script.Parent.Parent.Tp1 local tp2 = script.Parent.Parent.Tp2 local player1 = nil local player2 = nil function teleportPlayers(p1, p2) -- Teleport them end part1.Touched:Connect(function(obj) -- part1 touched local character = obj.Parent -- get the character if character:FindFirstChild("Humanoid") then -- make sure it's a player character player1 = game.Players:GetPlayerFromCharacter(character) -- get/set the player end end) part1.TouchEnded:Connect(function(obj) -- part1 touched ended local character = obj.Parent if character:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(character) if player == player1 then -- make sure the player that stopped touching is player1 player1 = nil -- set player1 to nil end end end) part2.Touched:Connect(function(obj) local character = obj.Parent if character:FindFirstChild("Humanoid") then player2 = game.Players:GetPlayerFromCharacter(character) end end) part2.TouchEnded:Connect(function(obj) local character = obj.Parent if character:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(character) if player == player2 then player2 = nil end end end) while true do wait() if player1 and player2 then -- check to see if player1 AND player2 are valid players teleportPlayers(player1, player2) -- teleport them player1 = nil -- reset values player2 = nil end end
In terms of one of them dying, what you could do would be inside of the teleportPlayers function is make a Player.Character.Humanoid.Died:Connect(function() ... end)
to check if either of them died.
https://wiki.roblox.com/index.php?title=API:Class/Humanoid/Died