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

Respawn, teleport player to random brick?

Asked by 10 years ago

Hiya, everyone! :)

This is my first post on Scripting Helpers- Thank you for your time and effort!

I have made a script (with much assistance) that attempts to teleport players (upon respawn) to a random brick, aptly named one,two,three.. until ten! Unfortunately, in solo mode testing this script fails to do anything- Even when I remove the group checking. Where did I go wrong? How is this fixed? Thank you!

-Chimo

local spawns = {workspace.one, workspace.two, workspace.three, workspace.four, workspace.five, 
workspace.six, workspace.seven, workspace.eight, workspace.nine, workspace.ten}


game.Players.PlayerAdded:connect(onPlayerRespawned)
function onPlayerRespawned(newPlayer)
    wait(1)
    if not newPlayer:GetRankInGroup(235869) == 2 then
    wait(0.1)
newPlayer.PlayerGui.Choose:remove()
local db = spawns[math.random(1, #spawns)]
        newPlayer.Character:findFirstChild("Torso").CFrame = CFrame.new(db.Position)     
 end end


 function onPlayerEntered(newPlayer)
    newPlayer.Changed:connect(function (property)
        if (property == "Character") then
            onPlayerRespawned(newPlayer)
        end
    end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

2 answers

Log in to vote
1
Answered by 10 years ago

Firstly, welcome to Scripting Helpers! I'm proud to see more people like you interested in scripting, it's a very versatile and powerful tool to use in game development.

All welcomes aside, the PlayerAdded event will not function correctly when you play solo. This is because the player is created before any of the scripts can execute (unless you use a LocalScript, but it's recommended not to use a LocalScript for a PlayerAdded event.)

To counter this, you'll want to add a snippet of code like this to end of your script:

for _,player in pairs(Players:GetPlayers()) do --Get all players already in the game (in this case, the solo player.)
     onPlayerEntered(player) --Fire the function for all players
end

This snippet of code basically makes sure that your function will fire for all players currently in the game (in this example, the solo player.)

I have made comments in the code which go through each of the processes that happen in the snippet of code provided. You may remove these comments by deleting all the green text in Studio when you insert the snippet of code into your script. (Comments are highlighted in green and usually start with --)

EDIT: Try this:

local spawns = {workspace.one, workspace.two, workspace.three, workspace.four, workspace.five, 
workspace.six, workspace.seven, workspace.eight, workspace.nine, workspace.ten}

function onPlayerRespawned(newPlayer)
    wait(1)
    if not newPlayer:GetRankInGroup(235869) == 2 then
    wait(0.1)
newPlayer.PlayerGui.Choose:remove()
local db = spawns[math.random(1, #spawns)]
        newPlayer.Character:WaitForChild("Torso").CFrame = CFrame.new(db.Position)     
 end 
end

function onPlayerEntered(newPlayer)
    onPlayerRespawned(newPlayer)
    newPlayer.CharacterAdded:connect(function(char) --Fires whenever the player's character is added or respawned.
        onPlayerRespawned(newPlayer)
    end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

for _,player in pairs(Players:GetPlayers()) do
     onPlayerEntered(player)
end

Happy scripting and good luck! Make sure to accept my answer if it helped!

0
It still doesn't work, I am so confused! But thank you for helping! :D chimo88 5 — 10y
0
Edited answer, try the new script. Spongocardo 1991 — 10y
0
Edit doesn't work 0.o I commend you for your assistance, however! chimo88 5 — 10y
0
You're welcome, and edited again. Spongocardo 1991 — 10y
Ad
Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

In addition to Spongocardo's answer, another error the code contained is situated in line 12.

Position contains Vector3 values. Since CFrame doesn't accept Vector3 values, change '.Position' to .CFrame'.

newPlayer.Character:findFirstChild("Torso").CFrame = CFrame.new(db.CFrame)

EDIT

Don't listen to what I just answered. You can use Vector3 values in a new CFrame.

You can also use CFrame values in a a new Vector3:

script.Parent.Position= Vector3.new(game.Workspace.Part.CFrame)

At least I've learned something today. xD

0
Thank you for assisting! I'm going to check in Play mode now. chimo88 5 — 10y
0
I could've sworn you could use Vector3 values inside of a new CFrame. The Vector3 would just be converted into a CFrame if I'm not mistaken. Spongocardo 1991 — 10y
0
Mhm- Well, the fact of the matter is I borrowed this heavily from a working script I use- In theory, the biggest mistake should've come from deciding what brick to teleport you to, but it seems like everything is bunked. xD chimo88 5 — 10y
0
...Huh, apparently Vector3 values can be used inside of a new CFrame. Redbullusa 1580 — 10y

Answer this question