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

Need Help With if true Command?

Asked by 5 years ago

I can't figure out why its not working!

moneyToGive = 10

debounce = false
script.Parent.Touched:connect(function(hit)
    wait(1)
if debounce == true then return end
player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player == nil then return end
stat = player:findFirstChild("leaderstats")
if stat == nil then return end

cash = stat:findFirstChild("Cash")
if cash == nil then return end
debounce = true 
wait(.1)
if game.Workspace.Desperado.Body.Hay.Transparency == 0 then
cash.Value = cash.Value + moneyToGive
wait(2)
debounce = false
wait(5)
game.Workspace.Desperado.Body.Hay.transparency = 1
end)

0
if player == nil then return end, your just checking if it returns is nil  -_- User#23365 30 — 5y
0
That didn't really help... Darkcraft10 20 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Problem

if you look on line 10, your doing if stat == nil then return end, the == operator is for checking if something is equal to another thing so your just checking if :GetPlayerFromCharacter() returns nil then execute. You'd need to use the ~= operator which checks if something is not equal to another thing, Though you dont need to do that since :GetPlayerFromCharacter() returns nil if a player doesnt exist by itself. You should add a if statement to check if the parent of the PartThatTouched parameter has a humanoid.

Deprecated functions and Clean code

:findFirstChild(), :connect(), transparency is deprecated dont use it, instead use :FindFirstChild(), :Connect() and Transparency. Also indent your code so its easier to read and looks cleaner and use local variables instead of global variables.

More Things

Firstly, your checking if the debounce is true, though it is false without any lines setting it to true before the if statement. You also forgot alot of ends for the if statements, and if you dont have output enabled, enable it as it'll make debugging way easier.

local moneyToGive = 10
local debounce = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") and debounce == false then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)

        if player then
            local stats = player.leaderstats 
            local cash = stats.Cash

            debounce = true
            wait(.1)

            if game.Workspace.Desperado.Body.Hay.Transparency == 0 then
                cash.Value = cash.Value + moneyToGive
                wait(2)
                debounce = false
                wait(5)
                game.Workspace.Desperado.Body.Hay.Transparency = 1
            end
        end
    end
end)
Ad

Answer this question