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

How to give points for touching block while the block is only a certain color and tool?

Asked by 2 years ago
Edited 2 years ago

I'm trying to make it so that when you run over the touched block with a certain tool the tool will only be removed and points will only be given for a certain color.

Right now whether it is purple or gray the tool will be removed and XP will be given. I only want XP to be given and the tool to be removed if the block is purple. The particle is also not working in this current version.

I had started with a removal code which worked great when I didn't care about the color of the block. I thought part of the problem was it didn't have a debounce so I added that. I tried elseif statements which will print into Output but does not stop the tool from being removed. I tried doubling the check for purple twice so maybe it would stop it somewhere, and no.

Basically it seems like it always thinks the current color of the part is 'purple' because the if statement isn't using it as a conditional. It will remove the part and give XP.

Here's the code as it is:

local expPart = script.Parent

local debounce = true

local purple = Color3.fromRGB(100, 0, 100)
local gray = Color3.fromRGB(67, 67, 67)

currentColor = expPart.Color

local function delivery(hit)
    if currentColor == purple and debounce then
        debounce = false
        local Humanoid = hit.Parent:FindFirestChild("Humanoid")
        if currentColor == purple and Humanoid and debounce == false then
            for i,item in pairs(hit.Parent:GetChildren()) do
                if item.Name == "Box" and item:IsA("Tool") then
                    local stats = game.Players[hit.Parent.Name].leaderstats
                    local expGive = stats:WaitForChild("XP")
                    expGive.Value = expGive.Value +10
                    local playerCharacter = game.Players[hit.Parent.Name].Character
                    local particle = Instance.new("ParticleEmitter")
                    particle.Color = ColorSequence.new(currentColor)
                    particle.Parent = playerCharacter:WaitForChild("Head")
                    debounce = true
                    wait(2)
                    particle:Destroy()
                    item:Destroy()
                end     
            end
            for i,item in pairs(game.Players[hit.Parent.Name].Backpack:GetChildren()) do
                if item.Name == "Box" and item:IsA("Tool") then
                    item:Destroy()
                    wait(2)
                    debounce = true
                end
            end
        end
        wait(2)
        debounce = true
    elseif currentColor == gray and debounce then
        debounce = false
        print("It is gray")
        wait(2)
        debounce = true
        return
    end
end

expPart.Touched:Connect(delivery)

while true do
    local randomTimer = math.random(1,22)
    expPart.Color = purple
    wait(10)
    expPart.Color = gray
    wait(randomTimer)
end

I started with this code originally:

expPart.Touched:connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if Humanoid and currentColor == purple then
        for i,item in pairs(hit.Parent:GetChildren()) do
            if item.Name == "Box" and item:IsA("Tool") then
                local stats = game.Players[hit.Parent.Name].leaderstats
                local expGive = stats:WaitForChild("XP")
                expGive.Value = expGive.Value +10
                local playerCharacter = game.Players[hit.Parent.Name].Character
                local particle = Instance.new("ParticleEmitter")
                particle.Color = ColorSequence.new(currentColor)
                particle.Parent = playerCharacter:WaitForChild("Head")
                wait(1)
                particle:Destroy()
                item:Destroy()
            end
        end
        for i,item in pairs(game.Players[hit.Parent.Name].Backpack:GetChildren()) do
            if item.Name == "Box" and item:IsA("Tool") then
                item:Destroy()          
            end
        end
    end 
end)

I appreciate all of you taking a look at this!

Answer this question