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

How to fix this script?

What I've tried to do is create a leaderboard stat that shows both the group that the person is home to and that person's rank in them. However, when trying to use this, the stat itself does not show (even with a blank value.

game.Players.PlayerAdded:connect(function(newPlayer)
	repeat wait() until newPlayer:findFirstChild("leaderstats")
	local Rank = Instance.new("StringValue", newPlayer.leaderstats)
	Rank.Name = "Rank"
	if newPlayer:IsInGroup(2553045) then
		Rank.Value = "MRR [Host]:" + newPlayer:GetRoleInGroup(2553045)
	elseif newPlayer:IsInGroup(316950) then
		Rank.Value = "Fatal Redemption [Ally]" + newPlayer:GetRoleInGroup(316950)
	elseif newPlayer:IsInGroup (914233) then
		Rank.Value = "Allied Airborne [Enemy] + newPlayer:GetRoleInGroup(914233)
	elseif newPlayer:IsInGroup() then
		Rank.Value = "Visitor/Floater"
	end
end)

I'm a bit new to roblox coding so just tell me if my assumptions are correct with the fact that you can't use the + to add in more text. Does anyone know how to fix this?

3 answers

✓ Accepted answer

The concatenation operator in Lua is .., so you're correct there. The problem, as I see it, is you're assuming that leaderstats is created for you, which it isn't. You're also missing a " on line 10.

game.Players.PlayerAdded:connect(function(newPlayer)
   Instance.new("IntValue", newPlayer).Name = "leaderstats"
    local Rank = Instance.new("StringValue", newPlayer.leaderstats)
    Rank.Name = "Rank"
    if newPlayer:IsInGroup(2553045) then
        Rank.Value = "MRR [Host]: " .. newPlayer:GetRoleInGroup(2553045)
    elseif newPlayer:IsInGroup(316950) then
        Rank.Value = "Fatal Redemption [Ally]: " .. newPlayer:GetRoleInGroup(316950)
    elseif newPlayer:IsInGroup(914233) then
        Rank.Value = "Allied Airborne [Enemy]: " .. newPlayer:GetRoleInGroup(914233)
    else --No need for an `if` here. if/elseif chains are the 'switch' statements of Lua. `else` is the default case.
        Rank.Value = "Visitor/Floater"
    end
end)

1 comment

0

Thank you so much, this is the most helpful answer that I have received. About closing the " (sorry, new to this site so I don't know how to make it like a code), I realized this shortly after making this post. Also, your line 2 doesn't appear to be needed as it seems that the script works (probably due to the parent script that is partnered with it). However, I'll keep this in mind if I make it

You can't use + to concatenate text in Lua. This is because 5 + "3" evaluates to 8 -- it also makes sure you meant that, as opposed to wanting 53.

You use the concatenation operator .. to join text.

You also have a syntax error on line 10; you didn't finish the string with a closing ".


It's probably a good idea to make variables for the different group ids since you repeat them.

The script is probably getting stuck before the creation, put a print statement before and after the repeat loop to make sure as I cannot see any point at which you create the "leaderstats" object.

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