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

if statement possibly breaking my script?

Asked by 3 years ago
local Players = game:GetService("Players")


local function onPartTouch(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    local player = Players:GetPlayerFromCharacter(partParent)

    if humanoid then
        if player.TokenFile.WaterfallToken1.Value == false then
            script.Parent.Transparency = 1
            script.Parent.CanCollide = false

            local leaderstats = player.leaderstats
            local findcoins = leaderstats and leaderstats:FindFirstChild("coinz")
            local coinz4m = math.random(10,50)

            if findcoins then
                findcoins.Value = findcoins.Value + coinz4m
                player.TokenFile.WaterfallToken1.Value = true
            end
        end
    end
end
script.Parent.Touched:Connect(onPartTouch)

Not sure if I'm accessing the model or the player in game.Players. It should be game.Players though. If you need any other scripts such as the leaderstats scripts, i'd be happy to supply them. I'm also not getting any errors, works fine without the

if player.TokenFile.WaterfallToken1.Value == false then
    --code here
end

but it is kind of a critical part of what I'm working on.

0
Other information required.Since just only seeing this script looks flawless TerranRecon 49 — 3y
0
Ok, ill send the other relating scripts SamZeKat 28 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You gotta check if the part's parent is actually the player 'cause sometimes the part touches the ground and stuff. Simply add an operator on line 9:

if humanoid and player then

Let me know if this works.

0
nope, as i said, it works perfectly fine w. out the If statement. When the If statement is in the code, it doesn't even run, SamZeKat 28 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

~Other Relating Scripts~

Waterfall Token Script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") 

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "TokenFile"
    local WCrate = Instance.new("BoolValue", Leaderstats)
    WCrate.Name = "WaterfallToken1"
    WCrate.Value = false
    --WCrate2
    local WCrate2 = Instance.new("BoolValue", Leaderstats)
    WCrate2.Name = "WaterfallToken2"
    WCrate2.Value = false

    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        WCrate.Value = Data 
    end
    -- WCrate2
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        WCrate2.Value = Data 
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.TokenFile.WaterfallToken1.Value) -- Change "Money" with your currency.
end)
---WCrate2
game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.TokenFile.WaterfallToken2.Value) -- Change "Money" with your currency.
end)


(intentionally bool value)

coinz leaderstats script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") 

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"
    local Currency = Instance.new("IntValue", Leaderstats)
    Currency.Name = "coinz"
    Currency.Value = 0

    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        Currency.Value = Data 
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.coinz.Value) 
end)

Answer this question