Why is this just working on the Server?

local plrs = game:GetService("Players")

local LevelUpHandler = require(script.LevelUpHandler)
local Upar = LevelUpHandler.LevelUp

local LoadStats = require(script.LoadStats)
local Carregar = LoadStats.LoadStat

plrs.PlayerAdded:Connect(function(plr)
	Carregar(plr)
	
	plr.CharacterAdded:Connect(function(chr)
		local hum = chr:WaitForChild("Humanoid")
		local leaderstats = plr:WaitForChild("leaderstats")
		local lv = leaderstats:WaitForChild("LOVE")
		
		hum.MaxHealth += (lv.Value * 5) + 20
		hum.Health = hum.MaxHealth
	end)
	
	local leaderstats = plr:WaitForChild("leaderstats")
	local lv = leaderstats:WaitForChild("LOVE")
	local xp = leaderstats:WaitForChild("XP")
	
	Upar(xp,lv,plr)
	xp:GetPropertyChangedSignal("Value"):Connect(function()
		task.wait()
		print("XP: ", tostring(xp.Value))
		Upar(xp, lv, plr)
	end)
end)

This Script is just working on server side even if the same has the PlayerAdded Event… Why?

PlayerAdded fires on both the client and server.

Then why is this just showing up for server?

Why is what just showing up?
123123123

I believe they mean why the script only functions on the server.

I would assume its because on the client, the script is added to the client after the player joins, so it wouldn’t detect the join of whichever client it is running on.

2 Likes

Oh, so how could I fix this? I am really confused I am new at scripting though…

This is a good point. I am also suspicious if maybe LoadStat contains data store calls, which only work on the server. One would have to step through it to see for sure.

1 Like

LoadStat is leaderstats, is a Module Script that “loads” the leaderstats, it is not a data save

If nostalgic is right then you need to move that function outside of Connect(). It should still be connected, but then it should also be called manually for the local player.

function YourFunction(plr)
	Carregar(plr)
	...
end

plrs.PlayerAdded:Connect(YourFunction)
YourFunction(plrs.LocalPlayer)
2 Likes

Player added will Not fire for the localplayer in a localscript. It can only detect other players joining.

That is because the localscripts will only load after playeradded fires for the local player. Therefore the localscripts miss the event associated with the localplayer

To fix this, remove the whole playeradded bracket:

local plr = game:GetService(“Players”).LocalPlayer

local LevelUpHandler = require(script.LevelUpHandler)
local Upar = LevelUpHandler.LevelUp

local LoadStats = require(script.LoadStats)
local Carregar = LoadStats.LoadStat

Carregar(plr)

plr.CharacterAdded:Connect(function(chr)
local hum = chr:WaitForChild(“Humanoid”)
local leaderstats = plr:WaitForChild(“leaderstats”)
local lv = leaderstats:WaitForChild(“LOVE”)

hum.MaxHealth += (lv.Value * 5) + 20
hum.Health = hum.MaxHealth

end)

local leaderstats = plr:WaitForChild(“leaderstats”)
local lv = leaderstats:WaitForChild(“LOVE”)
local xp = leaderstats:WaitForChild(“XP”)

Upar(xp,lv,plr)
xp:GetPropertyChangedSignal(“Value”):Connect(function()
task.wait()
print("XP: ", tostring(xp.Value))
Upar(xp, lv, plr)
end)

sorry for the bad editing, I’m doing this on mobile

1 Like

So I can’t do that?
So what do I do?

I just edited the reply, read it

Thank you! It should work, but why wasn’t it working before? Even though with the playeradded

I just realized the problem is not solved. I am not using local script to run the script, I am using a normal one. I am trying to make a leveling system althought it just loads for the server not for the player that was added.

ok, i need a little more to work with. what exactly is the unexpected result?

Nevermind I already found the error. The error was that I was trying to change the value on Client instead of Server, sorry for the inconvenience!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.