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

Script if/else not working correctly?

So I'm trying to create a button that will switch the swords in the players' inventories from Standard to Anti-Team kill. I have a "Different Weapons for Different Teams" script in so all I have to do is put the sword into the team itself and reset the players. What is going wrong is, I can toggle the button once and it works fine, but if I toggle it a second time, it stops working. The error I receive is: **LinkedSword is not a valid member of Team Script 'Workspace.Button.Script', Line 6 Stack End ** So it seems that the check of the toggle variable is not working like I want it to. Keep in mind, I have another toggle button that is formated the same way, but all it does is change a few things about a brick and disable/enable a script. And that one works. Help is appreciated, solutions are more so.

Button = script.Parent
toggle = false
Button.ClickDetector.MouseClick:connect(function()
	toggle = not toggle
	if toggle == true then
		game.Teams.Apostles.LinkedSword:remove()
		game.Teams.Raiders.LinkedSword:remove()
		game.Lighting.TeamedSword:clone().Parent = game.Teams.Apostles
		game.Lighting.TeamedSword:clone().Parent = game.Teams.Raiders
			for i, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
			local hum = player.Character:FindFirstChild('Humanoid')
			if hum then
				hum.Health = 0
		Button.BrickColor = BrickColor.Red()
		toggle = false
	else
		game.Teams.Apostles.TeamedSword:remove()
		game.Teams.Raiders.TeamedSword:remove()
		game.Lighting.LinkedSword:clone().Parent = game.Teams.Apostles
		game.Lighting.LinkedSword:clone().Parent = game.Teams.Raiders
			for i, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
			local hum = player.Character:FindFirstChild('Humanoid')
			if hum then
				hum.Health = 0
		Button.BrickColor = BrickColor.Green()	
end end end end end end end end)

1 comment

0

what is not toggle? on line 4. never seen it in lua

1 answer

Change the parent after the Clone() If you put .Parent on a function, it thinks you are referring to the object previously mentioned.

if toggle then
	local ClonedSword = SWORD:Clone()
	ClonedSword.Parent = Inventory
end

I recommend you use serverstorage over lighting

game:GetService('ServerStorage').OBJECT:Clone()

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