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

Help finding out why my script spawns people randomly in workspace?

Asked by 6 years ago

I've used this script in many cases, but lately, roblox keeps spawning people randomly in the sky, at random places, and not at my "plot" model with "BaseSpawnPoint" part in it. This is from my Tycoon.

So my issue, is the teleport part, where it teleports people to random places in workspace. Not to the specific tycoon part, that is bought as a spawn point.

I know that this will fail using R15, because it doesn't detect the torso (my game does not support R15 yet)

wait(3) --let the globals script load up

function onPlayerRespawn(property, player)
    --check if player owns a base
    local isOwner = false
    local plot = nil
    for i,v in pairs (shared.Bases) do
        if workspace[v].Owner.Value == player.Name then
            isOwner = true
            plot = workspace[v]
            break
        end
    end
    if isOwner == false then return end
    --check if player owns the teleport upgrade
    if not plot:findFirstChild("BaseSpawnPoint") then return end
    --calculate vector to the base spawn point pad
    local vector = plot.BaseSpawnPoint.Position - player.Character.Torso.Position
    --move player
    for i,v in pairs (player.Character:GetChildren()) do
        if v.className == "Part" then
            v.CFrame = v.CFrame + vector
        end
    end
    --play teleport sound
    plot.BaseSpawnPoint.Sound:Play()
    --sparklify player
    for i,v in pairs (player.Character:GetChildren()) do
        if v.className == "Part" then
            local sparkles = Instance.new("Sparkles")
            sparkles.Parent = v
            game:GetService( "Debris" ):AddItem( sparkles, 0.5 ) 
        end
    end
end

function onPlayerEntered(newPlayer)
    newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
end

game.Players.ChildAdded:connect(onPlayerEntered)
0
Before I check this to hard, is there an output? Bellyrium 310 — 6y
0
It doesn't print anything, but it does execute, but at random places in workspace. Master3395 10 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

You don't need to move every part of a player to a new location, you can just set the CFrame property of the HumanoidRootPart.

Plus, this randomness you're talking about doesn't seem to be random, from what I'm reading you've just done your maths wrong, and are placing a Character relative to their position from a spawnpoint and the basespawnpoint.

To fix this, all you have to do is set the HumanoidRootPart 3 studs above your basespawnpoint.

Like so

wait(3) --let the globals script load up

function onPlayerRespawn(property, player)
    --check if player owns a base
    local isOwner = false
    local plot = nil
    for i,v in pairs (shared.Bases) do
        if workspace[v].Owner.Value == player.Name then
            isOwner = true
            plot = workspace[v]
            break
        end
    end
    if isOwner == false then return end
    --check if player owns the teleport upgrade
    if not plot:findFirstChild("BaseSpawnPoint") then return end
    --calculate vector to the base spawn point pad
    local location = plot.BaseSpawnPoint.CFrame + Vector3.new(0,3,0)
    --move player
    if player.Character:FindFirstChild("HumanoidRootPart") then
        player.Character.HumanoidRootPart.CFrame = location
    end
    --play teleport sound
    plot.BaseSpawnPoint.Sound:Play()
    --sparklify player
    for i,v in pairs (player.Character:GetChildren()) do
        if v.className == "Part" then
            local sparkles = Instance.new("Sparkles")
            sparkles.Parent = v
            game:GetService( "Debris" ):AddItem( sparkles, 0.5 ) 
        end
    end
end

function onPlayerEntered(newPlayer)
    newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
end

game.Players.ChildAdded:connect(onPlayerEntered)

Hope this helps!

0
As a bonus, this also works with R15! M9_Sigma 35 — 6y
0
Will check if this is resolving my issue. Master3395 10 — 6y
Ad

Answer this question