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

Why don't this script work?

Asked by 10 years ago

I'm making a script that gives the player one of the random items in Lighting when they spawn, but it doesn't work please help!

game.Players.PlayerAdded:connect(function(player)
    player.Character.CharacterAdded:connect(function(char)
        local i = Instance.new("BoolValue",player)
        i.Name = "KnifeHolding"
        if i
        then end
      else
    i.Value = true
    local mr = math.random(1,4)
    if mr==1 then
        local clone = game.Lighting.Blade:Clone()
        clone.Parent = player.Backpack
    elseif mr==2
        local clone = game.Lighting.Bombos:Clone()
        clone.Parent = player.Backpack
    elseif mr==3
        local clone = game.Lighting.DualKnives:Clone()
        clone.Parent = player.Backpack
    elseif mr==4
        local clone = game.Lighting.InsaneKnife:Clone()
        clone.Parent = player.Backpack
    end)
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.Died:connect(function(died)
                i.Value = false
            end)
        end)

3 answers

Log in to vote
0
Answered by 10 years ago

It's supposed to be"if hum ~= nil then". And instead of "if i then end", you need to write "if i == false then (here should be the rest of your code, without the first else)"

Ad
Log in to vote
0
Answered by 10 years ago

This should work, assuming it doesn't interact with anything else. Comment if there are errors and I'll try to fix them.

game.Players.PlayerAdded:connect(function(player)
    player.Character.CharacterAdded:connect(function(char)
        local i = Instance.new("BoolValue", player)
        i.Name = "KnifeHolding"
        if i == false then --you didn't have an if for the else, and you don't need an else anyways.
            i.Value = true
            local mr = math.random(4) --it'll assume your starting at 1.
            if mr==1 then
                local clone = game.Lighting.Blade:Clone()
                clone.Parent = player.Backpack
            elseif mr==2
                local clone = game.Lighting.Bombos:Clone()
                clone.Parent = player.Backpack
            elseif mr==3
                local clone = game.Lighting.DualKnives:Clone()
                clone.Parent = player.Backpack
            elseif mr==4
                local clone = game.Lighting.InsaneKnife:Clone()
                clone.Parent = player.Backpack
            --else  --put these two lines in if you want to return if "i" is true.
                --return
            end
            local hum = char:FindFirstChild("Humanoid")
            if hum ~= nil then --hum ~= nil as stated by another answerer.
                hum.Died:connect(function(died)
                    i.Value = false
                end)
            end
        end)
    end)
end)
Log in to vote
-2
Answered by 10 years ago

Please post Output codes if there's any, if there's not please put that somewhere so we may better help you.

plr = game.Players.LocalPlayer
game.Players.PlayerAdded:connect(function(player)
    player.Character.CharacterAdded:connect(function(char)
        local i = Instance.new("BoolValue",player)
        i.Name = "KnifeHolding"
        if i
        then end
      else
    i.Value = true
    local mr = math.random(1,4)
    if mr==1 then
        local clone = game.Lighting.Blade:Clone()
        clone.Parent = plr.Backpack
    elseif mr==2
        local clone = game.Lighting.Bombos:Clone()
        clone.Parent = plr.Backpack
    elseif mr==3
        local clone = game.Lighting.DualKnives:Clone()
        clone.Parent = plr.Backpack
    elseif mr==4
        local clone = game.Lighting.InsaneKnife:Clone()
        clone.Parent = plr.Backpack
    end)
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.Died:connect(function(died)
                i.Value = false
            end)
        end)
0
None RolandStudio 115 — 10y
0
Ok, what script type are you using? TwoSlhue 5 — 10y
0
Script Public. RolandStudio 115 — 10y
0
Try a local script and try what i've edited my post into. TwoSlhue 5 — 10y
View all comments (3 more)
0
When you edited it , it gives me and error of On line 8 an end is above it doing something RolandStudio 115 — 10y
0
Post the error. TwoSlhue 5 — 10y
0
Workspace.Script:8: 'end' expected (to close 'function' at line 3) near 'else' RolandStudio 115 — 10y

Answer this question