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

Why does this only change its brickcolor once?

Asked by
AltNature 169
5 years ago

First of all something you NEED TO KNOW is that I AM NOT IN THIS GROUP!

No errors in the output. Why does this only change brickcolor once?

local part = script.Parent
local debounce = false
local players = game:GetService("Players")
local e = math.random(1,10)

game.Workspace.TouchPart.Touched:Connect(function(hit) 
 if not debounce and hit.Parent:FindFirstChild("Humanoid") and players:FindFirstChild(hit.Parent.Name) then
debounce = true

    local plar = players:FindFirstChild(hit.Parent.Name)

   if plar:IsInGroup(3467972) then

    wait(1) 

     part.BrickColor = BrickColor.Green()

   local e = math.random(1,10)


elseif e >= 5 then

      part.BrickColor = BrickColor.Green()

 else

      part.BrickColor = BrickColor.Red()

end

debounce = false

end

end)

I hope you can help!

2 answers

Log in to vote
0
Answered by
oftenz 367 Moderation Voter
5 years ago

You have your random value inside of your IsInGroup.

This means there will be no random values, fixed version:

local part = script.Parent
debounce = false
local players = game:GetService("Players")
e = math.random(1,10)

game.Workspace.TouchPart.Touched:Connect(function(hit) 
 if not debounce and hit.Parent:FindFirstChild("Humanoid") and players:FindFirstChild(hit.Parent.Name) then
debounce = true

    local plar = players:FindFirstChild(hit.Parent.Name)

   if plar:IsInGroup(3467972) then

    wait(1) 

     part.BrickColor = BrickColor.Green()

end
e = math.random(1,10)
if e >= 5 then

      part.BrickColor = BrickColor.Green()

 else

      part.BrickColor = BrickColor.Red()

end

debounce = false

end

end)

Ad
Log in to vote
0
Answered by 5 years ago

Don't use FindFirstChild to check if an object with the specified name exists in the Players service. This will error if you attempt to use a Player object only method, event, or property on something that isn't a Player. Use GetPlayerFromCharacter instead. You also have an elseif statement that checks for something completely different, when elseif runs code if its if statement returns false.

local part = script.Parent
local debounce = false
local players = game:GetService("Players")
local e = math.random(1,10)
local getPlayerFromCharacter

getPlayerFromCharacter = function(model)
    local player = players:GetPlayerFromCharacter(model)

    if player then
        return player
    else
        return nil
    end
end

game.Workspace.TouchPart.Touched:Connect(function(hit) 
    if not debounce then 
        debounce = true
        local plar = getPlayerFromCharacter(hit.Parent)

        if plar:IsInGroup(3467972) then
            wait(1) 
            part.BrickColor = BrickColor.Green()
            local e = math.random(1,10)

            if e > 5 then
                part.BrickColor = BrickColor.Green()
            else -- checks if e < 5
               part.BrickColor = BrickColor.Red()
            end
        end

        wait(1) -- put time to reset here
        debounce = false -- set to false so we can do this again
    end
end)


Answer this question