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
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.
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.
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.
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