I’m trying to make an event where if players touch 8 bricks hidden around the map, they’ll be awarded a badge. All the bricks are single pieces in a folder, if that helps any. I’m not a coder, but my main coder had to bail on me for a while and my backup couldn’t figure out how to get this test script to work properly and I just really could use some help.
I’ll post what the test script is, but it’s a local script located in StarterPlayerScripts and the test is just on a single part in the workspace. The part is supposed to turn blue and then go transparent as a check that the part has been touched. The part turns blue, but we can’t seem to get the transparent part to work at all
local Players = game:GetService("Players");
local badgeID = 000000 -- Change this to the Badge ID
local function awardBadge()
local hasBadge = false
local player = Players.LocalPlayer
-- Check if the player already has the badge
local success, message = pcall(function()
hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge: " .. tostring(message))
return
end
if hasBadge == false then
BadgeService:AwardBadge(player.UserId, badgeID)
end
end
local part1 = game.Workspace.Part
local part1Check = false
local function part1Touch()
part1Check = true
end
part1.TouchEnded:Connect (function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
part1.Color = Color3.fromRGB(34,61,255)
part1Touch()
wait()
end
end)
if part1.Color == Color3.fromRGB(34,61,255) then
part1.Transparency = 0.5
end
You should handle that stuff in a server script since all of this can be done on the server safely than the client.
You can use a table to put those 8 parts in then use a pairs iterator to give them touched events and also have another table that tracks all players and when they touch any of the 8 parts, they would get that part added to their index in the table then see if they got 8 parts to award the badge after counting a part touched.
EDIT: Actually you can only use BadgeService for the server anyways.
You set the part1.Transparency in an if-statement in the end. The Connect() function doesn’t block the thread, which means after connecting the function, it goes on to check the color. Of course, since it just connected the event, the if-statement would always turn out to be false and the transparency is never set!
Your solution is you want to set the transparency in the same place where you set the color: when the part was touched.
You can do something like this for the awarding badge after touching 8 bricks
local partsTouched = {}
local function tableContains(tbl, element)
for _, v in ipairs(tbl) do
if (rawequal(v, element)) then
return true;
end
end
return false;
end
function isAllTouched()
if #partsTouched == 8 then
print("Found all 8 parts!")
-- award badge
end
end
for _,v in pairs(workspace.Parts:GetChildren()) do
v.Touched:Connect(function()
-- make a check if it's touched by a player (find for humanoid)
if not tableContains(partsTouched, v.Name) then -- checks if table already has the part
table.insert(partsTouched, v.Name)
print("inserted" .. v.Name)
isAllTouched()
end
end)
end
For the turning blue and go transparent, just do it inside this scope.
if not tableContains(partsTouched, v.Name) then -- checks if table already has the part
table.insert(partsTouched, v.Name)
print("inserted" .. v.Name)
-- turn blue ( v.BrickColor = BrickColor.new("Medium blue") )
-- turn transparent ( v.Transparency = 0.5 )
isAllTouched()
end
You have to keep in mind that what I wrote is just a general and rough idea. It’s up to you to play with it around. You probably want to use it as a localscript (cause only for one player), and fire an event for the badge awarding.
Thank you very much! While I’m not too versed in Lua yet, I will try it out! The blue then transparent code was mainly just a test to see if the bricks were responding to being touched
By the way you don’t have to check if the player has the badge or not, it won’t award it a second time. The wiki says this so you can skip the badge check.