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)
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!
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