Sign in to follow this  
danielbsantanna

Help to Check First Spawn (Solved)

Recommended Posts

I'm trying to check if is player first spawn for do some things for Client and server. I already can do for Server, but I don't know how to do this for client.

I think that when the function is executed by the server, it will not be executed by the client. When Cliente_Side is outslide OnNewSpawn function, is executed every time a player join server.

English is not my main language so may have translation erros.

My code looks like:

local OnPlayerSpawn = function(player)
	player.prev_OnNewSpawn = player.OnNewSpawn
	player.OnNewSpawn = function()
		
		if SERVER_SIDE then
			--do Things in server_side
		end
    	
		if CLIENT_SIDE then
			--do Things in client_side
		end
		 
		if player.prev_OnNewSpawn ~= nil then
			player:prev_OnNewSpawn()
			player.prev_OnNewSpawn = nil
		end
		
	end
end

local function OnPlayerPostInit(player)
	OnPlayerSpawn(player)
end

AddPlayerPostInit(OnPlayerPostInit)

This must be something simple, but I'm missing it.

 

Edited by danielbsantanna
  • Big Ups 1

Share this post


Link to post
Share on other sites
10 hours ago, penguin0616 said:

if TheWorld.ismastersim then
  -- SERVER SIDE
else
  -- CLIENT SIDE
end

 

I didn't post the complete code, but I already got this. My problem is how to check if is first spawn for Client_Side too, because OnNewSpawn Function is only executing when is server side.

 

local TheNet = GLOBAL.TheNet
local SERVER_SIDE, DEDICATED_SIDE, CLIENT_SIDE, ONLY_CLIENT_SIDE
if TheNet:GetIsServer() then
    SERVER_SIDE = true
    if TheNet:IsDedicated() then
        DEDICATED_SIDE = true
		CLIENT_SIDE = false
    else
        CLIENT_SIDE = true
		DEDICATED_SIDE = false
    end
elseif TheNet:GetIsClient() then
    SERVER_SIDE = false
    CLIENT_SIDE = true
    ONLY_CLIENT_SIDE = true
end

 

Share this post


Link to post
Share on other sites
AndRay    20

I *think* you can use OnEntityReplicated from the player_classified prefab, that, if I'm not mistaken, sets the events for the client. It might trigger on load, though; the code is not as clear on what happens on the loaded world (either it creates all known entities, or it sets up the classified prefab again from the server prefab; the second case would call the function again)

You might also be able to use the player IDs to create a "has spawned" lookup table.

Edited by AndRay

Share this post


Link to post
Share on other sites

I Just find a workaround, maybe the correct way to do this.

 

It's just a exemple code.

local TheNet = GLOBAL.TheNet
local SERVER_SIDE, DEDICATED_SIDE, CLIENT_SIDE, ONLY_CLIENT_SIDE
if TheNet:GetIsServer() then
    SERVER_SIDE = true
    if TheNet:IsDedicated() then
        DEDICATED_SIDE = true
		CLIENT_SIDE = false
    else
        CLIENT_SIDE = true
		DEDICATED_SIDE = false
    end
elseif TheNet:GetIsClient() then
    SERVER_SIDE = false
    CLIENT_SIDE = true
    ONLY_CLIENT_SIDE = true
end


local OnPlayerSpawn = function(player)
	player.prev_onNewSpawnClient = player.OnNewSpawn
	player.prev_OnNewSpawn = player.OnNewSpawn
	player.OnNewSpawn = function()
		player.myNetVarFirstSpawn:set(true)
		DoStartStuff(player)
		if player.prev_OnNewSpawn ~= nil then
			player:prev_OnNewSpawn()
			player.prev_OnNewSpawn = nil
		end 
	end
end

local function OnPlayerPostInit(player)
	OnPlayerSpawn(player)
	player.myNetVarFirstSpawn = _G.net_bool(player.GUID, "PlayerFirstSpawn", "PlayerFirstSpawnDirty") -- true or false
    player.myNetVarFirstSpawn:set(false) 
    if CLIENT_SIDE then
        player:ListenForEvent("PlayerFirstSpawnDirty", function(player) 
			if player.myNetVarFirstSpawn:value() then DoStartStuff(player) end
		end)
    end
end

AddPlayerPostInit(OnPlayerPostInit)

How to set the topic as solved?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this