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)
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)"
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)
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)