I am trying to create a sword fighting game where there is a sword fighting arena. To enter the arena, you must click a part witch activates the teleport script. Here is what I have.
function onClicked() local c = game.Players:GetChildren() for i = 1, #c do c[i].Character.Torso.CFrame = CFrame.new(script.Parent.Parent.Teleport.Position) end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
The only problem with this script, is that it teleports all players in the server, instead of just the player who clicked the part. I am very new to scripting, so if someone could just give me a script that has all the current things my current one does, but with only the specified player that clicked the part being teleported, that would be awesome!
MouseClick
event passes the Player
who clicked as a parameter, so use that.local function onClicked(player) player.Character:SetPrimaryPartCFrame( script.Parent.Parent.Teleport.CFrame ) end script.Parent.ClickDetector.MouseClick:Connect(onClicked)
RBXScriptSignal:connect()
is deprecated, use RBXScriptSignal:Connect()
instead.local ClickDetector = script.Parent local teleport = ClickDetector.Parent:WaitForChild("Teleport", 2) -- Never assume something is there by directly referencing it without checking. if not teleport then print("no teleport") return end -- Stop the script if there's no teleport -- more for testing to make sure you used your parents correctly local pos = teleport.Position ClickDetector.MouseClick:Connect(function(playerwhoClicked) -- Anonymous function passes the player who clicked as a Parameter. local char = playerwhoClicked.Character -- Define character once, so you don't have to write it again. local hum = char:findFirstChild("Humanoid") if hum and hum.Health > 0 then -- Check if we're dead, don't teleport if we're dead. char:MoveTo(pos) end end)