Hey there! I am making different lobbies for my game, and when I try giving players a value for which lobby, they won’t spawn right.
My code:
Value
local PlacePlayer = Instance.new("StringValue")
PlacePlayer.Name = "PlayerPlace"
PlacePlayer.Value = "Park"
PlacePlayer.Parent = player
Spawner
function newCharacter(character)
local player = game.Players:GetPlayerFromCharacter(character)
local trail = player.PlayerTrail.Value
local speed = player.leaderstats.Speed.Value
if player:WaitForChild("PlayerPlace").Value == "Park" then
print("Park")
character:MoveTo(game.Workspace.Spawners.Park:FindFirstChildOfClass("SpawnLocation").Position)
elseif player:WaitForChild("PlayerPlace").Value == "Desert" then
print("Desert")
character:MoveTo(game.Workspace.Spawners.Desert:FindFirstChildOfClass("SpawnLocation").Position)
end
--other code........
end
robloxapp-20210217-1624160.wmv (1.3 MB)
This video shows me spawning at the desert area, then the park, then the desert area again, even though he clearly has the park value.
Workspace
It also does not print anything when the player first joins, but does when the player resets.
Does anyone know why this happens?
Sorry it took me forever to reply, here is my full code:
--creates and stores player stats in the roblox server
--Create a spot in Roblox servers to store data
local datastore = game:GetService("DataStoreService"):GetDataStore("SpeedSimData")
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeID = 2124670700
local boughRequest = game.ReplicatedStorage:WaitForChild("BoughtRequest")
_G.boughtArray = {}
--Runs when a player enters the game
function onPlayerEntered(player)
--Create a key to store player stats
local initKey = "user_" .. player.userId .. "_init"
local stepsKey = "user_" .. player.userId .. "_steps"
local pointsKey = "user_" .. player.userId .. "_points"
local speedKey = "user_" .. player.userId .. "_speed"
local trailKey = "user_" .. player.userId .. "_trail"
local animKey = "user_" .. player.userId .. "_anim"
local petKey = "user_" .. player.userId .. "_pet"
local xpKey = "user_" .. player.userId .. "_xp"
local levelKey = "user_" .. player.userId .. "_level"
local orbsBadgeKey = "user_" .. player.userId .. "_orbsBadge"
local winsBadgeKey = "user_" .. player.userId .. "_winsBadge"
local particleKey = "user_" .. player.userId .. "_particle"
local boughtKey = "user_" .. player.userId .. "_bought"
--Creates default stats for new players
if(datastore:GetAsync(initKey) == nil) then
datastore:SetAsync(initKey, true)
datastore:SetAsync(stepsKey, 0)
datastore:SetAsync(pointsKey, 0)
datastore:SetAsync(speedKey, 1)
datastore:SetAsync(trailKey, "DefaultTrail")
datastore:SetAsync(animKey, "DefaultAnim")
datastore:SetAsync(petKey, "DefaultPet")
datastore:SetAsync(xpKey, 0)
datastore:SetAsync(levelKey, 1)
datastore:SetAsync(orbsBadgeKey, 0)
datastore:SetAsync(winsBadgeKey, 0)
datastore:SetAsync(particleKey, "DefaultParticleTrail")
datastore:SetAsync(boughtKey, {})
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeID)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, badgeID)
print("Badge Awarded to " .. player.Name)
end)
if not awarded then
warn("Error while awarding badge:", errorMessage)
end
end
else
warn("Error while fetching badge info!")
end
end
if (datastore:GetAsync(trailKey) == nil) then
datastore:SetAsync(trailKey, "DefaultTrail")
end
if (datastore:GetAsync(animKey) == nil) then
datastore:SetAsync(animKey, "DefaultAnim")
end
if (datastore:GetAsync(animKey) == "DefaultAnimation") then
datastore:SetAsync(animKey, "DefaultAnim")
end
if (datastore:GetAsync(petKey) == nil) then
datastore:SetAsync(petKey, "DefaultPet")
end
if (datastore:GetAsync(xpKey) == nil) then
datastore:SetAsync(xpKey, 0)
end
if (datastore:GetAsync(levelKey) == nil) then
datastore:SetAsync(levelKey, 1)
end
if (datastore:GetAsync(orbsBadgeKey) == nil) then
datastore:SetAsync(orbsBadgeKey, 0)
end
if (datastore:GetAsync(winsBadgeKey) == nil) then
datastore:SetAsync(winsBadgeKey, 0)
end
if (datastore:GetAsync(particleKey) == nil) then
datastore:SetAsync(particleKey, "DefaultParticleTrail")
end
if (datastore:GetAsync(boughtKey) == nil) then
datastore:SetAsync(boughtKey, {})
end
--game.ReplicatedStorage.BadgePlayedEvent:Fire(player)
print(player)
--Load in player's last saved stats
local init = datastore:GetAsync(initKey)
local steps = datastore:GetAsync(stepsKey)
local points = datastore:GetAsync(pointsKey)
local speed = datastore:GetAsync(speedKey)
local trail = datastore:GetAsync(trailKey)
local anim = datastore:GetAsync(animKey)
local pet = datastore:GetAsync(petKey)
local xp = datastore:GetAsync(xpKey)
local level = datastore:GetAsync(levelKey)
local orbsBadge = datastore:GetAsync(orbsBadgeKey)
local winsBadge = datastore:GetAsync(winsBadgeKey)
local particle = datastore:GetAsync(particleKey)
local bought = datastore:GetAsync(boughtKey)
_G.boughtArray[player.userId] = bought
print(anim)
--Creates leaderboard
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stepsBoard = Instance.new("IntValue")
stepsBoard.Name = "Steps"
stepsBoard.Value = steps
stepsBoard.Parent = leaderstats
local speedBoard = Instance.new("IntValue")
speedBoard.Name = "Speed"
speedBoard.Value = speed
speedBoard.Parent = leaderstats
local pointsBoard = Instance.new("IntValue")
pointsBoard.Name = "Points"
pointsBoard.Value = points
pointsBoard.Parent = leaderstats
--Tracks player trail
local playerTrail = Instance.new('StringValue')
playerTrail.Name = "PlayerTrail"
playerTrail.Value = trail
playerTrail.Parent = player
local playerAnim = Instance.new('StringValue')
playerAnim.Name = "PlayerAnim"
playerAnim.Value = anim
playerAnim.Parent = player
local playerPet = Instance.new('StringValue')
playerPet.Name = "PlayerPet"
playerPet.Value = pet
playerPet.Parent = player
--Tracks whether player is in a race
local playerIsRacing = Instance.new("BoolValue")
playerIsRacing.Name = "PlayerIsRacing"
playerIsRacing.Value = false
playerIsRacing.Parent = player
local playerHasTouched = Instance.new("BoolValue")
playerHasTouched.Name = "PlayerHasTouched"
playerHasTouched.Value = false
playerHasTouched.Parent = player
local playerHasPass2x = Instance.new("BoolValue")
playerHasPass2x.Name = "PlayerHasPass2x"
playerHasPass2x.Value = false
playerHasPass2x.Parent = player
local playerHasPassDoublePoints = Instance.new("BoolValue")
playerHasPassDoublePoints.Name = "PlayerHasPassDoublePoints"
playerHasPassDoublePoints.Value = false
playerHasPassDoublePoints.Parent = player
local playerHasPassConvert = Instance.new("BoolValue")
playerHasPassConvert.Name = "PlayerHasPassConvert"
playerHasPassConvert.Value = false
playerHasPassConvert.Parent = player
local playerHasPassOtherConvert = Instance.new("BoolValue")
playerHasPassOtherConvert.Name = "PlayerHasPassOtherConvert"
playerHasPassOtherConvert.Value = false
playerHasPassOtherConvert.Parent = player
local playerHasPassStepGain = Instance.new("BoolValue")
playerHasPassStepGain.Name = "PlayerHasPassStepGain"
playerHasPassStepGain.Value = false
playerHasPassStepGain.Parent = player
local playerHasPassOtherStepGain = Instance.new("BoolValue")
playerHasPassOtherStepGain.Name = "PlayerHasPassOtherStepGain"
playerHasPassOtherStepGain.Value = false
playerHasPassOtherStepGain.Parent = player
local playerResetCharacter = Instance.new("BoolValue")
playerResetCharacter.Name = "PlayerResetCharacter"
playerResetCharacter.Value = false
playerResetCharacter.Parent = player
local playerNoRaceMode = Instance.new("BoolValue")
playerNoRaceMode.Name = "PlayerNoRaceMode"
playerNoRaceMode.Value = false
playerNoRaceMode.Parent = player
local MorePoints = Instance.new("IntValue")
MorePoints.Name = "MorePoints"
MorePoints.Value = 0
MorePoints.Parent = player
--Tracks speed orb steps value
local orbGuiInt = Instance.new("IntValue")
orbGuiInt.Name = "StepsBonusCheck"
orbGuiInt.Value = 0
orbGuiInt.Parent = player
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = xp
XP.Parent = player
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = level
Level.Parent = player
local OrbsBadge = Instance.new("IntValue")
OrbsBadge.Name = "OrbsBadgeCount"
OrbsBadge.Value = orbsBadge
OrbsBadge.Parent = player
local WinsBadge = Instance.new("IntValue")
WinsBadge.Name = "WinsBadgeCount"
WinsBadge.Value = winsBadge
WinsBadge.Parent = player
local ParticlePlayer = Instance.new("StringValue")
ParticlePlayer.Name = "PlayerParticleTrail"
ParticlePlayer.Value = particle
ParticlePlayer.Parent = player
local PlacePlayer = Instance.new("StringValue")
PlacePlayer.Name = "PlayerPlace"
PlacePlayer.Value = "Park"
PlacePlayer.Parent = player
--Tracks speed orb color
local orbGuiBCV = Instance.new("BrickColorValue")
orbGuiBCV.Name = "BCVCheck"
orbGuiBCV.Value = BrickColor.Black()
orbGuiBCV.Parent = player
--MainLoop Instance Creation
local serverStorage = game:GetService("ServerStorage")
local mainLoopInstance = serverStorage.MainLoop:Clone()
mainLoopInstance.Name = "MainLoop" .. player.UserId
mainLoopInstance.Disabled = false
mainLoopInstance.Parent = game:GetService("ServerScriptService")
-- Connects to newCharacter function
player.CharacterAdded:Connect(newCharacter)
end
game.Players.PlayerAdded:Connect(onPlayerEntered)
function onPlayerLeaving(player)
local userId = player.UserId
local leaderstats = player:WaitForChild("leaderstats")
local steps = leaderstats.Steps.Value
local speed = leaderstats.Speed.Value
local points = leaderstats.Points.Value
local trail = player.PlayerTrail.Value
local anim = player.PlayerAnim.Value
local pet = player.PlayerPet.Value
local xp = player.XP.Value
local level = player.Level.Value
local orbsBadge = player.OrbsBadgeCount.Value
local winsBadge = player.WinsBadgeCount.Value
local particle = player.PlayerParticleTrail.Value
local bought = _G.boughtArray[player.userId]
local stepsKey = "user_" .. player.userId .. "_steps"
local pointsKey = "user_" .. player.userId .. "_points"
local speedKey = "user_" .. player.userId .. "_speed"
local trailKey = "user_" .. player.userId .. "_trail"
local animKey = "user_" .. player.userId .. "_anim"
local petKey = "user_" .. player.userId .. "_pet"
local xpKey = "user_" .. player.userId .. "_xp"
local levelKey = "user_" .. player.userId .. "_level"
local orbsBadgeKey = "user_" .. player.userId .. "_orbsBadge"
local winsBadgeKey = "user_" .. player.userId .. "_winsBadge"
local particleKey = "user_" .. player.userId .. "_particle"
local boughtKey = "user_" .. player.userId .. "_bought"
datastore:SetAsync(stepsKey, steps)
datastore:SetAsync(pointsKey, points)
datastore:SetAsync(speedKey, speed)
datastore:SetAsync(trailKey, trail)
datastore:SetAsync(animKey, anim)
datastore:SetAsync(petKey, pet)
datastore:SetAsync(xpKey, xp)
datastore:SetAsync(levelKey, level)
datastore:SetAsync(orbsBadgeKey, orbsBadge)
datastore:SetAsync(winsBadgeKey, winsBadge)
datastore:SetAsync(particleKey, particle)
datastore:SetAsync(boughtKey, bought)
end
game.Players.PlayerRemoving:Connect(onPlayerLeaving)
function newCharacter(character)
local player = game.Players:GetPlayerFromCharacter(character)
local trail = player.PlayerTrail.Value
local speed = player.leaderstats.Speed.Value
if player:WaitForChild("PlayerPlace").Value == "Park" then
print("Park")
character:MoveTo(game.Workspace.Spawners.Park:FindFirstChildOfClass("SpawnLocation").Position)
elseif player:WaitForChild("PlayerPlace").Value == "Desert" then
print("Desert")
character:MoveTo(game.Workspace.Spawners.Desert:FindFirstChildOfClass("SpawnLocation").Position)
end
local humanoid = character:WaitForChild("Humanoid")
if player.PlayerIsRacing.Value == true then
player.PlayerIsRacing.Value = false
game.ReplicatedStorage.GameEndEvent:Fire(player)
end
game:GetService("RunService").Stepped:Wait()
local storedTrail = game.ReplicatedStorage[trail]
local playerTrail = storedTrail:Clone()
playerTrail.Parent = character.HumanoidRootPart
local emmiter = game.ReplicatedStorage:FindFirstChild(player.PlayerParticleTrail.Value)
if character:FindFirstChild("UpperTorso") then
playerTrail.Attachment0 = character.Head.FaceFrontAttachment
playerTrail.Attachment1 = character.UpperTorso.WaistRigAttachment
local Middle = character.UpperTorso.BodyBackAttachment
local Right = character.UpperTorso.RightShoulderRigAttachment
local Left = character.UpperTorso.LeftShoulderRigAttachment
local emmiterClone = emmiter:Clone()
emmiterClone.Parent = Middle
local emmiterClone2 = emmiter:Clone()
emmiterClone2.Parent = Right
local emmiterClone3 = emmiter:Clone()
emmiterClone3.Parent = Left
else
playerTrail.Attachment0 = character.Head.FaceFrontAttachment
playerTrail.Attachment1 = character.HumanoidRootPart.RootRigAttachment
end
humanoid.WalkSpeed = speed
end
function onBoughtRequest(player)
print(_G.boughtArray[player.userId])
while (_G.boughtArray[player.userId] == nil) do
wait(0.1)
end
return _G.boughtArray[player.userId]
end
boughRequest.OnServerInvoke = onBoughtRequest
So, this may do nothing at all, but try putting ALL of your connections (outside of functions) at the bottom of your code. I’m not sure off the top of my head if this can create a scope issue, but I would at least try it.
Unfortunately, this did not work, what is mind boggling about this is that it will still print, and do all the other stuff in the characterAdded function…
Now, I know there is way to make them spawn based on the teams, but it clogs up the leaderboard… But that would be my solution, if I could take away the leaderboard part of it.
You have a lot of datastore requests in your script, try putting all of your data in a table and save/load that instead if having a separate datastore for everything.