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

Making this script Multiplayer friendly...?

Asked by 10 years ago

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.

1
Well, that's because you made it teleport all players, because every player has the connection in it. Tkdriverx 514 — 10y
1
Do you just want one player to be teleported? Tkdriverx 514 — 10y
0
I've updated my answer. adark 5487 — 10y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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)
1
It isn't a tool. Here. I updated my original post with the script. User#5565 45 — 10y
1
Thanks! Works now! User#5565 45 — 10y
Ad

Answer this question