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

How do I make this script just teleport the player, not all players?

How do I make this script just teleport the player and not all the players? I need help. I know this is not a request site but this is the only place where I can ask. This is a normal Script

local BuggyLoc = CFrame.new(-106.428, 121.84, -194.854)

script.Parent.MouseButton1Click:connect(function(Tele)
	for i, v in pairs(game.Players:GetPlayers()) do
    v.Character:SetPrimaryPartCFrame(BuggyLoc)
	end
end)

5 comments

0

Is it teleporting you to the right place, even though all players are teleporting.

0

Yes it is teleporting me to the car's seat, but all players are being teleported there and I don't want that.

0

So basically I think instead of using for i,v in pairs, you can use the parameter in the Tele I'll give an example in my answer.

0

local BuggyLoc = CFrame.new(-106.428, 121.84, -194.854) clickDetector = script.Parent local function onMouseClick(player) v = player v.Character:SetPrimaryPartCFrame(BuggyLoc) end clickDetector.MouseClick:connect(onMouseClick)

0

Oh by the way is this is in a local script?

2 answers

✓ Accepted answer

Well, you're using a script for what seems to be a job cut out for a LocalScript. .MouseButton1Down is meant as something registered on the Client. However, if you use a LocalScript, the teleport will only be registered on the client. We need to use a RemoteEvent to register it on the server. You can read more about RemoteEvents and Server stuff here. First, you need to create a RemoteEvent in the ReplicatedStorage of your game. Name it something.

This is what your local script should look like.

script.Parent.MouseButton1Click:connect(function()
	game.ReplicatedStorage.TeleportEvent:FireServer()
end)

This is what your server script should look like, put it in the ServerScriptStorage.

game.ReplicatedStorage.TeleportEvent.OnServerEvent:connect(function(plr)
	local BuggyLoc = CFrame.new(-106.428, 121.84, -194.854)
	plr.Character.SetPrimaryPartCFrame(BuggyLoc)
end)

Hope that helped out, consider accepting this answer. If not, comment below and I'll be sure to help out further.

Assuming this is a server script and not a local script

local BuggyLoc = CFrame.new(-106.428, 121.84, -194.854)

script.Parent.MouseButton1Click:connect(function(Tele)
	for i, v in pairs(game.Players:GetPlayers()) do
		if v.Character then --Checking if the player has a Character
			if v.Character == script.Parent.Parent then --Checking if this player's character is the player's character
    				v.Character:SetPrimaryPartCFrame(BuggyLoc)
			end
		end
	end
end)

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