Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do i make a teleporter script only teleport ONE player, not ALL players?

Asked by 5 years ago
Edited 5 years ago

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!

2 answers

Log in to vote
0
Answered by 5 years ago

The 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)

On a side note, RBXScriptSignal:connect() is deprecated, use RBXScriptSignal:Connect() instead.

Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago
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)
0
Don't give bad methods and deprecated code. Either teach good code or don't teach at all. User#19524 175 — 5y
0
'Sorry that I made a mistake like most humans do, but no need to be rude about it.' Azarth 3141 — 5y

Answer this question