Not Checking If Player Owns Badge

Good Evening! (or morning/afternoon)
Tonight I am working on saving a currency by using badges.

How this works is, when the player hits the part, it gets deleted and they are given the currency. When they leave there is a script that will check if the player owns the badge for that part, if they do it will give them the currency and delete the part. This rejoining part does not seem to work. (doesnt delete the part or give the currency) The script is in a folder in ServerScriptService, and it is a normal script (should be a local script because it has to delete the part but I was seeing if it would work as a normal one).

Here is the script!

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2126944041

game:GetService("Players").PlayerAdded:Connect(function(player)
	if BadgeService:UserHasBadge(player. UserId, BadgeId) then
		game.Players.leaderstats.Frogs.Value = 	game.Players.leaderstats.Frogs.Value + 1
		game.Workspace.FrogsFolder.RegularFrog:Destroy()
	end
end)

If anyone could help fix this that would be great! I swear I did this right no idea what is wrong

few typos and incorrect reference try this

	local BadgeService = game:GetService("BadgeService")
	local BadgeId = 2126944041

	game:GetService("Players").PlayerAdded:Connect(function(player)
		if BadgeService:UserHasBadge(player.UserId, BadgeId) then  -- fixed spacing on Player.Userid here
			player.leaderstats.Frogs.Value = player.leaderstats.Frogs.Value + 1   -- needed to use player here to reference not game.Players
			game.Workspace.FrogsFolder.RegularFrog:Destroy()
		end
	end)
3 Likes

oh wait I think I saw the error just on accident lol… extra spaces for some reason
small dumb mistakes I somehow did not notice

1 Like

Is it okay to have this in a localscript? I just put it in one and it isn’t working for some reason.

you would want this on server side so it can change the frogs value on the server put it in server script in serverscriptservice

Alright but then if it is on the server side it will delete the part for everyone instead of just one player specifically.

You could do this on the client in a local script

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2126944041
local Player = game.Players.LocalPlayer

if BadgeService:UserHasBadge(Player.UserId, BadgeId) then  -- fixed spacing on Player.Userid here
	game.Workspace.FrogsFolder.RegularFrog:Destroy()
end

and on that server script you have to just remove the destroy line and it will still add +1

1 Like

Oh ok thank you so much!

(extra letters)

1 Like

should I do this for the other script as well?

you could create a remote event & fire it to the specific player like so :

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2126944041

	game:GetService("Players").PlayerAdded:Connect(function(player)
		if BadgeService:UserHasBadge(player.UserId, BadgeId) then 
			player.leaderstats.Frogs.Value = player.leaderstats.Frogs.Value + 1   -- needed to use player here to reference not game.Players
			game.ReplicatedStorage.RemovePart:FireClient(player)
		end
	end)

and in the localscript :

local Player = game.Players.LocalPlayer

game.ReplicatedStorage.RemovePart.OnClientEvent()
game.Workspace.FrogsFolder.RegularFrog:Destroy()
end)
1 Like

Just tried doing this and it doesn’t delete it. It does go in ServerScriptService right?

no you can put this in the startergui in a local script or even in StarterPlayerScripts but i normally do startergui from old habit and it reloads those when character reloads

1 Like

Oooohh, thank you. Sorry I was fixing other stuff.

1 Like

UserHasBadge is deprecated.

https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadgeAsync