Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why don't this script work?

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

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)

7 comments

0

None

0

Ok, what script type are you using?

0

Script Public.

0

Try a local script and try what i've edited my post into.

0

When you edited it , it gives me and error of On line 8 an end is above it doing something

0

Post the error.

0

Workspace.Script:8: 'end' expected (to close 'function' at line 3) near 'else'

This discussion is preserved as it was. New answers, comments, and votes are closed.