Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Sword-fighting and teleporting -- how would I do this?

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

2 comments

0

just make 2 bool values and 1 brick the will set one to true and the other one will set the 2nd one to true then make a if statment, if the 2 bools are set to true then teleport them

0

sorry can you expand on this?

1 answer

✓ Accepted answer

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

4 comments

0

Thank you! :)

0

Thanks very much, do you think you'll be able to provide a teleport function to the specific "Teleport1 part"? I tried to look at the wiki but I got confused.

0

If you want to teleport players to a place, you would need the place ID, and the players' User ID along with the TeleportService, if you just want to teleport to a location, then you would use CFrame.

0

^ I asked on the discord and figured this one out, thank you very much though. xoxo Working on the death thing now :)

This discussion is preserved as it was. New answers, comments, and votes are closed.