Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Solved] Why is the collectable coin not working?

Asked by 3 years ago
Edited 3 years ago

I was trying to make a collectable ring, everything was working. Once I collect the ring, it turns invisible. However the ring is not turning opaque again even though I have wrote the script to turn it's transparency to 0.

There are no errors in the output either

Here is the code:

local db = true
script.Parent.Touched:Connect(function(hit) -- The player hits the part
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then -- Just to check if its not nil
        if db == true then -- Just to check if the db is true
            db = false
            local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player
            player.leaderstats.Rings.Value = player.leaderstats.Rings.Value + 1 -- Giving Currency
            script.Parent.Transparency = 1 -- Making it invisible
            script.Parent.CanCollide = false -- Now the player can walk through the ring
            script.Parent.Bubble:Play() -- Playing the sound
            wait(2)
-- Respawning the ring:
            db = true 
            script.Parent.Transparency = 0
            script.Parent.CanCollide = true
        end
    end
end)

Anything I can do?

0
any error? or stuffs? xXLegendGamerz16Xx 231 — 3y
0
script.Parent.Touched:Connect(function(hit) local player = hit.Parent:FindFirstChild("Humanoid") local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr.leaderstats.Coins.Value >= 0 then wait() script.Disabled = true script.Parent.Transparency = 1 script.Parent.CanCollide = false plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +1 wait(1) script.Parent.Transpar MattVSNNL 620 — 3y
0
Thanks a lot! It worked! CoinWield 47 — 3y

2 answers

Log in to vote
-1
Answered by
MattVSNNL 620 Moderation Voter
3 years ago
Edited 3 years ago
script.Parent.Touched:Connect(function(hit)
    local player = hit.Parent:FindFirstChild("Humanoid")
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr.leaderstats.Coins.Value >= 0 then
        wait()
        script.Disabled = true
        script.Parent.Transparency = 1
        script.Parent.CanCollide = false
        plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +1
        wait(1)
        script.Parent.Transparency = 0
        script.Parent.CanCollide = true
        script.Disabled = false
    end
end)
Ad
Log in to vote
0
Answered by 3 years ago

The script works fine for me, It's either 1: Your sound is probably misspelled or something. 2: You do not have leader stats or the leader stats is misspelled again.

0
The sound isn't misspelled and I can confirm that because the sound works. The leaderstats isn't misspelled either. What I tried was to remove the if statement " if hit.Parent:FindFirstChild("Humanoid") ~= nil then". Once I removed that, I got an error! It says "attempt to index nil with leaderstats". I believe the way to fix this is to change how you get the player. (Next Comment) CoinWield 47 — 3y
0
So, Is there a way to get the player when he/she touches a part? I used game.Players:GetPlayerFromCharacter(hit.Parent), but if that causes an error, there must be another way. CoinWield 47 — 3y

Answer this question