If player in group then give leaderstat cash script not working

I want the script to make it so that if the localplayer is in the group, it gives the player 300 cash in leaderstats. The script to do that is not working. (For context, I am the owner of that group.)

image
(other cash is from another script)

Here is the code:

local Player = game.Players.LocalPlayer
local groupId = 4565262
local minimumRank = 1

if Player:GetRankInGroup(groupId) >= minimumRank then
	Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 300
else
	print("noob")
end

Do you have the right rank number? because I think the owner rank is like 255

thats the minimum rank you have to be to get the cash
image

oh, hmm, then im not to sure, maybe look on youtube? just found this Roblox | How to make a Group Cash System | 2019 [FE] - YouTube

You’re doing this within a localscript I assume from the local Player = game.Players.LocalPlayer. This will only change it for the client and no one else, see this article..

On a serverscript, put this code:

local groupId = 4565262
local minimumRank = 1

game.Players.PlayerAdded:Connect(function(Player)
    if Player:GetRankInGroup(groupId) >= minimumRank then
	    Player:WaitForChild("leaderstats").Cash.Value += 300
    else
	    print("noob")
     end
end)
1 Like

Okay this is because you are running this code from a localscript, which is the source of your error. If you update A value on the client the server will not recognize / observe what had occured. Instead you want to bridge the gap between server and client by doing this.

So in the localscript you would have …

local player = game.Players.LocalPlayer

local addPointsRemote = game.ReplicatedStorage.AddPoints

addPointsRemote:FireServer() -- this will tell the server that you need to add points

and in the server script you would have something like this
local addPointsRemote = game.ReplicatedStorage.AddPoints

local groupID = 4565262

local minRank = 1

addPointsRemote.OnServerEvent:connect(function(sender)

if sender:GetRankInGroup(groupID) >= minRank then
if sender:FindFirstChild("leaderstats"):FindFirstChild("Cash") then

sender.leaderstats.Cash.Value += 300

end
end
end)

Hope that helps, Boston. :slight_smile:

P.S. Just to clarify @Wizrred 's solution works just as well, it just really depends what you are trying to use it for. If you want ti to be something that the user’s client can call upon more than once and won’t just happen when they join the server than you can use my method.

1 Like

The problem with that method is that exploiters can take control of that remote and give themselves the cash an infinite amount of times. Remember, never trust the client! It’s best to get the player from the server and do everything there instead. :slight_smile:

image
it was originally like this

edit: nvm my dev was looking over my shoulder and changed it lol

Well, of course I was just giving him a more foolproof method :stuck_out_tongue:

1 Like

You can’t call game.Players.LocalPlayer from the server. Only the client can do that, hence the “LocalPlayer”.

nvm my dev was looking over my shoulder and changed it lol ty

1 Like