You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I have been making my script and it seems to work in studio, however when I run it in the actual game, it does not work, the script is one that changes where the player goes to a spawnlocation depending on what subplace they joined from, and if not joining from a select few, going to a default spawn location, it did used to not work in studio but moving it around fixed it and has not bugged out in studio since. I think the bug is affecting the entire serverscriptservice, as when it does correctly work, a lot more messages open up in console. I also moved the script to a separate baseplate and a separate subplace in my game and it seemed to work perfectly. -
What is the issue? Include screenshots / videos if possible!
This is the code for the main script that goes into serverscriptservice:
print("Main game place spawn management script started.")
local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local DataStoreService = game:GetService("DataStoreService")
local LastWorldDataStore = DataStoreService:GetDataStore("LastWorldDataStore")
-- Define the spawn points for each subplace
local SpawnLocations = {
[116464308401502] = "RealitySpawn", -- Replace 12345678 with Place1ID
[94184163171789] = "GrassSpawn", -- Replace 87654321 with Place2ID
}
local DefaultSpawn = "DefaultSpawn" -- Name of the default spawn location
-- Function to set player spawn location based on last world
local function onPlayerAdded(player)
print("Player added:", player.Name)
local success, lastWorld = pcall(function()
return LastWorldDataStore:GetAsync(player.UserId)
end)
local spawnName = DefaultSpawn -- Default spawn location
if success then
print("Retrieved last world:", lastWorld)
if lastWorld and SpawnLocations[lastWorld] then
spawnName = SpawnLocations[lastWorld]
end
else
print("Failed to retrieve last world data:", lastWorld)
end
print("Setting spawn to:", spawnName)
local spawnLocation = game.Workspace:FindFirstChild(spawnName)
if spawnLocation and spawnLocation:IsA("SpawnLocation") then
print("Found spawn location:", spawnName)
-- Temporarily set the player's spawn point to the desired SpawnLocation
player.RespawnLocation = spawnLocation
-- Ensure the character spawns at the correct location
player:LoadCharacter()
-- Reset RespawnLocation after character spawns
player.RespawnLocation = nil
else
print("Spawn location not found or not a SpawnLocation:", spawnName)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- Function to update the last world data when teleporting
local function onPlayerTeleport(player, teleportData)
print("Player teleporting:", player.Name)
local placeId = teleportData.SourcePlaceId
-- Save the last world data
local success, errorMessage = pcall(function()
LastWorldDataStore:SetAsync(player.UserId, placeId)
end)
if success then
print("Saved last world data for player:", player.Name, "PlaceId:", placeId)
else
print("Failed to save last world data for player " .. player.UserId .. ": " .. errorMessage)
end
end
-- Connect the teleport event to update the last world data
TeleportService.TeleportInitFailed:Connect(function(player, teleportData)
print("Teleport Init Failed:", player.Name)
onPlayerTeleport(player, teleportData)
end)
TeleportService.TeleportInitSuccessful:Connect(function(player, teleportData)
print("Teleport Init Successful:", player.Name)
onPlayerTeleport(player, teleportData)
end)
print("Spawn management script loaded successfully.")
I also have this script for when the player teleports to add the datastore that the player is coming from a specific world:
local TeleportService = game:GetService("TeleportService")
local gameID = 94184163171789
function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions:SetTeleportData({ SourcePlaceId = game.PlaceId })
-- Teleport the player to the main game place with the teleport data
TeleportService:TeleportAsync(gameID, { player }, teleportOptions)
end
end
script.Parent.Touched:connect(onTouched)
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?**
I have looked all over the internet for answers, and nothing is working so far, I have tried disabling every script besides the required ones, changed how it is coded, added print scripts at the direct top, and more, and it has not worked at all, for all I know, this could be a studio bug or something uncontrollable like that.
Any help on this is very appreciated, as I have spent far too long struggling on it.