Hello Developers! I am having trouble with this script . It is supposed to disappear when someone joins the game, and then reappear after a random amount of time. Right now, it disappears when I join, but never comes back. Here it is:
(There is another script making the leaderboard which works!)
local cooldown = math.random(5, 10)
local Players = game:GetService("Players")
local gShard = script.Parent
local cando = false
gShard.Transparency = 1
gShard.CanCollide = false
local function collected()
cando = false
gShard.Transparency = 1
gShard.CanCollide = false
end
local function getready()
wait(cooldown)
cando = true
gShard.Transparency = 0.1
gShard.CanCollide = true
end
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
collected()
local player = Players:GetPlayerFromCharacter(partParent)
local leaderstats = player.leaderstats
local gStat = leaderstats and leaderstats:FindFirstChild("G Shards")
if gStat then
gStat.Value = gStat.Value + 1
getready()
end
end
end
if cando == true then
gShard.Touched:Connect(onPartTouch)
end
Hello, first of all it seems like you are not using PlayerAdded event, which you need for what you said in the post. But looking at the script you want the gShard to disappear when it is touched and collected.
So, this probably happens because you’re not using the Debounce properly to prevent that any player can touch it again and execute the whole function multiple times, also I saw your math.random() function is outside the function, so every time it will get the same number.
I redone the script to what I understood:
local Players = game:GetService("Players")
local gShard = script.Parent
local Seed = Random.new()
local cando = false
local function Collected()
if not cando then return end
gShard.Transparency = 1
gShard.CanCollide = false
cando = false
end
local function GetReady()
local cooldown = Seed:NextInteger(5, 10)
task.wait(cooldown)
cando = true
gShard.Transparency = 0.1
gShard.CanCollide = true
end
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
Collected()
local player = Players:GetPlayerFromCharacter(partParent)
local leaderstats = player.leaderstats
local gStat = leaderstats and leaderstats:FindFirstChild("G Shards")
if gStat then
gStat.Value += 1
GetReady()
end
end
end
gShard.Touched:Connect(onPartTouch)
robloxapp-20210813-1523464.wmv (4.8 MB) Hey! I inputted your script, and it didn’t go as intended… Every time I touched the item, it disappeared. And then it reappeared when touch ended… I got like 800 on the leaderstats. It is clearly able to access the leaderstats because it is changing the number, but not as provoked… Something else is wrong, unless you didn’t understand what I was trying to achieve…
I want it so when the game starts, it waits (random time) seconds before appearing, and when a player gets it, it disappears for (random time) seconds, and then reappears when the (random time) seconds are up. I want it so that the player can only get it when Cando is equal to true. It is supposed to check that every time a player touches it, but of course, a player could only touch it if property Cancolide were to be equal to true.
This should work if there are any problems just reply.
local Players = game:GetService("Players")
local gShard = script.Parent
local Seed = Random.new()
local cando = false
local function Collected()
gShard.Transparency = 1
gShard.CanCollide = false
cando = true
end
local function GetReady()
local cooldown = Seed:NextInteger(5, 10)
task.wait(cooldown)
cando = false
gShard.Transparency = 0.1
gShard.CanCollide = true
end
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid and not cando then
Collected()
local player = Players:GetPlayerFromCharacter(partParent)
local leaderstats = player.leaderstats
local gStat = leaderstats and leaderstats:FindFirstChild("G Shards")
if gStat then
gStat.Value += 1
GetReady()
end
end
end
gShard.Touched:Connect(onPartTouch)