How to make it check when a player joins?

Hi, so im making a progress bar and I want it so that whenever it detects that a player’s leaderstat gets changed, it tweens its position to where its supposed to be. However, I also want it to check when the player joins. Right now, it is not doing that!

Help needed!

local player = game.Players.LocalPlayer
local playerimg = game.Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size48x48)
local img = script.Parent.ImageLabel
img.Image = playerimg
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local maxStages = 73 -- Put stages whatever you want

player:WaitForChild("leaderstats"):WaitForChild("Stage"):GetPropertyChangedSignal("Value"):Connect(function()
	local tween = ts:Create(img, ti, {Position = UDim2.new(0.98/(maxStages/player.leaderstats.Stage.Value), 0, 0.05, 0)})
	tween:Play()
	script.Parent.Indicator.Text = "Stage: " ..tostring(player.leaderstats.Stage.Value)
end)

Thanks so much!

you should try PlayerAdded.
Players | Roblox Creator Documentation
I’m not sure if this works in local scripts or not because I’ve never used it in one

1 Like

I tried that, however it is a local script and it didn’t work.

1 Like

have you tried remote events or maybe remote functions?

I have not. Also I don’t think that would work.

1 Like

maybe you could try something like:

game.Players.Changed

or have a loop of :GetChildren()

Hello! You can only change a player’s leaderstats using a server script. Doing it with a local script wouldn’t work!

There is no point in checking when the player joins because all local scripts run when the player joins. It seems you do not know the difference between local and server scripts. You should learn about it here. It’s important!

In order to change the player’s leaderstats you will have to do it through a server script. But since these are 2 different script types, in order for them to communicate, and pass on information (like when a leaderstats value changes), you will have to use RemoteEvents.

SERVER SCRIPT

local leaderstatsChanged = game.ReplicatedStorage.leaderstatsChanged -- remote event
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player:WaitForChild("leaderstats").Stage:GetPropertyChangedSignal("Value"):Connect(function()
		leaderstatsChanged:FireClient(player) -- We are telling the player's localscript that the stage changed.
	end)
end)

In the server script above, the moment a player joins, the first thing we are doing is basically putting a constant tracker on that stage value. So if it ever changes, the new and changed value will be sent to the local script, where you can do whatever you want with it.

LOCAL SCRIPT

local leaderstatsChanged = game.ReplicatedStorage.leaderstatsChanged -- remote event

local player = game.Players.LocalPlayer
local playerimg = game.Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size48x48)
local img = script.Parent.ImageLabel
img.Image = playerimg
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local maxStages = 73 -- Put stages whatever you want

leaderstatsChanged.OnClientEvent:Connect(function(stage) -- the local script receives the server scrip's message that the Stage has changed
	local tween = ts:Create(img, ti, {Position = UDim2.new(0.98/(maxStages/stage), 0, 0.05, 0)})
	tween:Play()
	script.Parent.Indicator.Text = "Stage: " ..tostring(stage)
end)

In the local script above, I didn’t change much of your code. All I did was get the remote event, so that whenever the server script detected a change in the player’s leaderstats stage value, it would pass on that value into the same local script, where it would be used for tweeting or whatever…

P.S. Make sure you add a RemoteEvent into ReplicatedStorage, so that the code actually works! :slight_smile:

Let me know if this helps and if there are any errors or other things you might need help with! I tried to cover everything in your post :smiley: