Script works in Studio, does not in-game

I’m attempting to create a script that gives the player a tool when clicked if they have the required badge. However, it just does not do so at all and only sometimes prints to the output, other times it does not. And this never seems to work in-game, but does so in studio.

This is the troublesome code:

local Tool = ("Bolt")
local Storage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2147768506
local clickDetector = Instance.new("ClickDetector") 
--local clickDetector = script.Parent.ClickDetector 
local TP = script.Parent.Parent.TP2

clickDetector.MouseClick:Connect(function(Player)
	local playerId = game.Players:GetUserIdFromNameAsync(Player)
	print("BOLT!") -- These don't print in-game, but print in studio
	print(Player)
	print(playerId)
	if BadgeService:UserHasBadgeAsync(playerId, BadgeId) then
		Player.Character:MoveTo(TP.Position)
		local ToolClone = Tool:Clone()
		ToolClone.Parent = Player.Backpack
	end
end)

clickDetector.Parent = script.Parent

What could be going wrong in this code?

3 Likes

Try changing this to this:

local playerId = Player.UserId

If that doesn’t work can you show the output?

Yeah this seems to work, but the issue is that this does not print in the in-game output(not testing in studio) And the script doesn’t work, as if it doesn’t enter past that point.

When testing a badge in studio, you earn the badge even while testing. You could try removing the badge from your inventory and trying it again?

Yes, the badge was removed from my inventory, and tested on an alt as well.

This might sound a bit silly but, did you make sure you actually publish to Roblox?

I think that’s a fair claim to make since this is being done on a friend’s game and not my own with team create. But yes, it’s been published.

The badge may not be created by the game. Head to the game and scroll down. Do you see the badge?

Yes, the badge is in fact created by the game.

Have you made sure that the badge id is the correct id?

Yes, this as well. The badge id was checked.

Is the script a server-side script or a client-side script?

I was thinking that maybe this is where the issue might be. This is a server-sided script, and a large part of this issue is working in studio and not in-game.

What seems to not be working? The badge giver, or the gear giver?

The gear giver is not working. Nor are those things being printed.

Maybe try setting the parent of the clickdetector before the function? If that doesn’t work, try adding the clickdetector before using it and define it in a variable.

The reason why your gear giver isnt working is because you have the tool set as a string, not an actual tool. As for why the badge giver isnt working i’m not exactly sure, I really need to see the output to see the errors.

I have changed it from a string now:

local ToolNames = {"Bolt"}
local Storage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2147768506
local clickDetector = Instance.new("ClickDetector") 
local TP = script.Parent.Parent.TP2

clickDetector.MouseClick:Connect(function(Player)
	local playerId = Player.UserId
	if BadgeService:UserHasBadgeAsync(playerId, BadgeId) then
		print("Has badge")
		Player.Character:MoveTo(TP.Position)
		if Player and Player.Character then
			local Backpack = Player:WaitForChild("Backpack")
			for i = 1, #ToolNames do
				local Tool = Storage:FindFirstChild(ToolNames[i])
				if Tool then
					Tool:clone().Parent = Backpack
				end
			end
		end
	end
end)

clickDetector.Parent = script.Parent -- Parent the click detector to the part

Yet this only works in studio and still never in-game. What could be the problem?

have you checked if the badge is enabled, if it is not then you can’t really award it without inside studio if even doing that it doesn’t work then add the following line after
local playerid = player.userid
print(playerId)
to be sure that it is getting the user id and nothing else ,
and i don’t really recommend to make the badge give inside of each part that will give the badge, i would highly recommend to use collection service

Yeah, the badge is enabled, and it is indeed printing the player ID in that line of code. The strangest part is, this only sometimes seems to work in studio, reloading the game in studio causes it to work, and this never works in-game. I don’t understand why this is happening

1 Like