Hey developers, I have been making a quick script just for experience reasons, and I was wondering if there was anyway I could access a variable which is inside the function, and access it outside the function.
Here is my script.
coroutine.wrap(function()
while true do
wait(1)
local randomKey = math.random(1, 500)
end
end)()
I’ve tried using a global variable, but that didn’t work. I also tried using _G., but I don’t believe that would work and have little to no experience using it.
Any tips or suggestions on how to fix my problem would be greatly appreicated.
Thanks.
Removing the local will make it a script-global variable, meaning it can be used after there. You don’t need to declare it at the top if you remove the local unless you use it outside the coroutine in which case I don’t think it will work since different threads.
In which case you will either need to declare it at the top or something else.
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 2130071622 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return 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!")
return
end
if hasBadge then
badgedude = player.Name
end
end
wait(10)
print(badgedude)
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)