Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to refresh information on leaderboard/stats on respawn?

local playerStats = {}
 
 game.Players.PlayerAdded:connect(function(player)
     local leaderstats = Instance.new("Model", player)
     leaderstats.Name = "leaderstats"
 
     local grouprank = Instance.new("StringValue", leaderstats)
     grouprank.Name = "Rank"
	 local rank = player:GetRoleInGroup(3074970) 
	
     if rank ~= 0 then
		 grouprank.Value = rank
 	 else
		 grouprank.Value = "Guest"
	 end

 end)

I am very new to scripting, and just learning off of free scripts I find, etc. I've been very puzzled as I want to be able the game to recheck the player's group rank when they respawn, however I just cannot figure out how to do it.

2 answers

The PlayerAdded event only fires when a player joins the server, not every time the player respawns.

A suitable event for what you're trying to do would be CharacterAdded, which is connected to the player. Since PlayerAdded is a good way to get the player when a new player joins the server we can combine the two to make a function which will fire every time a player respawns.

From the wiki: ChracterAdded

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character.Humanoid.WalkSpeed = 25
	end)
end)

You can use this example with your code you have already, which looks pretty good! Just insert it below the CharacterAdded event and you should be good to go.

All the best!

1 comment

0

I really appreciate the help! I tried what you suggested, however the group rank is still the same when the character is reset (obviously I did change the rank via the group). Any other ideas? :c

I can help you out here. You made a couple mistakes

  1. You used :GetRoleInGroup() which returns the role of the player, not the rank number. Second off, you created a model for leaderstats when you should've created an IntValue. Here's your script fixed:
local playerStats = {}

 game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(char) -- This checks to see when the character is added. When a player dies, it's character is removed and then added back. This will make any code inside of player.CharacterAdded run whenever the player resets or dies.
	     local leaderstats = Instance.new("IntValue", player)
	     leaderstats.Name = "leaderstats"
	
	     local grouprank = Instance.new("StringValue", leaderstats)
	     grouprank.Name = "Rank"
	     local rank = player:GetRoleInGroup(3074970) 
	
	     grouprank.Value = rank
	end)

 end)

1 comment

0

I really appreciate the help! I tried what you suggested, however the group rank is still the same when the character is reset (obviously I did change the rank via the group). Any other ideas? :c

This discussion is preserved as it was. New answers, comments, and votes are closed.