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

Making this script Multiplayer friendly...?

Okay. In my game, you can shoot a "windball". When it hits this, it teleports them over to these coordinates. The only problem: It teleports all the players! Please tell me how to fix it. This is my script:

game.Players.PlayerAdded:connect(function(Player) 
	local player = Player.Name
	script.Parent.Touched:connect(function(part)
		if part.Name == "windball" then
			local tp = workspace:findFirstChild(player)
			tp:MoveTo(Vector3.new(245.8, 2, -205))
		end
	end)
end)

It does work, so don't say it doesn't.

WindBall script (LocalScript):

player = game.Players.LocalPlayer
char = player.Character
torso = char.Torso
mouse = player:GetMouse()

function press(key)
	key = key:lower()
	if key == "q" then
		local Ball = Instance.new("Part")
		Ball.Parent = workspace
		Ball.Shape = "Ball"
		Ball.Size = Vector3.new(5, 5, 5)
		Ball.BrickColor = BrickColor.new("Institutional White")
		Ball.Name = "windball"
		Ball.Transparency = .4
		Ball.TopSurface = 0
		Ball.BottomSurface = 0
		Ball.CFrame = torso.CFrame * CFrame.new(0,0.5,-5)
		local Bv = Instance.new("BodyVelocity")
		Bv.Parent = Ball
		Bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
		Bv.velocity = torso.CFrame.lookVector * 100
		game.Debris:AddItem(Ball,4)
		wait(3)
    	end 
	end
mouse.KeyDown:connect(press)

It gets put in the players backpack from another script.

3 comments

1

Well, that's because you made it teleport all players, because every player has the connection in it.

1

Do you just want one player to be teleported?

0

I've updated my answer.

1 answer

✓ Accepted answer

This is a logical issue, not actually a bug in the code.

The PlayerAdded event fires for every player, so every single player gets that Touched event connected for them. Since there's no checking if the Player you're teleporting is the Player who threw the 'windball', all Players get teleported.

The easiest way I can think to fix this is to have the 'windball' create an ObjectValue inside of itself, with the Value set to the Player's Character that threw it.

-snip-

Just add these two lines to the LocalScript (assuming Filtering is disabled) between lines 22 and 23:

Instance.new("ObjectValue", Ball).Name = "Owner"
Ball.Owner.Value = char

And change your original Script to this:

script.Parent.Touched:connect(function(part)
	    if part.Name == "windball" then
		local tp = windball:FindFirstChild("Owner")
		if tp then
			tp.Value:MoveTo(Vector3.new(245.8, 2, -205))
		end
	end
end)

2 comments

1

It isn't a tool. Here. I updated my original post with the script.

1

Thanks! Works now!

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