ServerScriptService script not working in Game, but working in Studio

You can write your topic however you want, but you need to answer these questions:

  1. 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.

  2. 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)
  1. 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.

3 Likes

what does the print() tell you? is the placeId correct

1 Like

the print does not say anything in the normal game and in studio it says the normal script started stuff as it is supposed to. The place id is where the player teleports, which does work, and the script detects the place the player is already at with the teleporting, that script seems to work on both versions, it mainly the serverscriptservice script that is causing issues.

1 Like

Just to check - have you published it?
(I have encountered cases where someone just forgets to hit publish)

yes, i have published the place many times, and each time the same thing happens, I have even tested to make sure it was by adding or deleting parts to see a difference and they were there

Can you please go in game, where you believ it should be published, and run the following command in console:

for _, child in game:GetService("ServerScriptService") do
	print(child.Name)
end

and tell us whether the script shows up there.

I just ran it in multiple areas, nothing was printed every time

Have you got any free models in the game, or any plugins installed? I’m suspecting there may be some maliciously acting code.

that is what I had thought too, however every single model and part was searched and nothing changed, it could be a plugin however, which i have not checked as much, wouldn’t explain though why it does work in other places in my game and not this specific one.

If you do ctrl + shift + F, and search for require, are there any results? (other than ones that you yourself wrote?)

Nothing came up when I searched require.

Odd. To check whether it is because of a plugin or because of a model, can you please run this code in the Studio command bar? Then, publish it and run the code I provided earlier in game again.

Let me know what the result of the code from earlier is.

After you run the code, you can run the second script which undoes the change:

-- First Script:
for _, script in game:GetDescendants() do
	if script:IsA("Script") and script.Enabled then
		script.Enabled = false
		script:SetAttribute("Enabled_3436707", true) -- just random values
	end
end
--Second Script:
for _, script in game:GetDescendants() do
	if script:IsA("Script") and script:GetAttribute("Enabled_3436707") then
		script.Enabled = true
		script:SetAttribute("Enabled_3436707", nil) 
	end
end
-- Script from earlier
for _, child in game:GetService("ServerScriptService") do
	print(child.Name)
end

(The first script is just disabling all server scripts, and the second one will reenable them)

I tested it and nothing was still printed, took me a second to do because I have not used command bar in a while.

That’s quite bizarre. Just to ensure it isn’t a plugin, can you please disable all installed plugins and try publishing it again?

so you mean on live, it does not even print("Main game place spawn management script started.") ?

and could you screenshot in the Explorer where this control script is located please?

I’m not entirely sure why it isn’t working, but you could change the whole thing up and use TeleportService:TeleportToSpawnByName() on the other end. I’m pretty sure you just set spawnName to the Name property of the spawnLocation you want the player to be teleported to, and it will spawn them there when they arrive in the other server.

There doesn’t really seem to be anything wrong with the code, but DataStore delays could cause an issue. If you need to keep the datastore system and don’t want to use the previously mentioned method, I would recommend using game:GetService("MemoryStoreService") instead since it has faster save times and similar methods.

Have you made sure that Access to API Services and Allow Third Party Teleports are enabled in the game settings?

I disabled all of them, and nothing different happened.

I had API Services enabled, but not Allow Third Party Teleports, I enabled it but nothing different happened.

The message does not even spawn at all in the actual game, this is where the script is located, it is the “Spawn” script.
Screenshot 2025-02-02 100309