I cant figure out why this script isnt working

ive been going crazy for the past 2 hours
ive tried everything. global variables, normal variables, spreading it across many more scripts, but NOTHING is working

im trying to make a sword switch modes when you press q
localscript in the tool

local UID = game:GetService("UserInputService")
local ToolEquipped = false

script.Parent.Equipped:Connect(function()
	ToolEquipped = true
end)

script.Parent.Unequipped:Connect(function()
	ToolEquipped = false
end)

UID.InputBegan:Connect(function(key, typing)
	if not typing then
		if key.KeyCode == Enum.KeyCode.Q and ToolEquipped then
			script.Parent.SwordSwitch:FireServer()
		end
	end
end)

it detects the q press just fine, and here’s my script for switching mode
serverscript in the tool

Mode = "Attack"

script.Parent.SwordSwitch.OnServerEvent:Connect(function(plr)
	print("received")
	if Mode == "Attack" then
		Mode = "Knockback"
		script.Parent.Handle.Color = Color3.fromRGB(0,0,255)
	end
	if Mode == "Knockback" then
		Mode = "Heal"
		script.Parent.Handle.Color = Color3.fromRGB(0,255,0)
	end
	if Mode == "Heal" then
		Mode = "Attack"
		script.Parent.Handle.Color = Color3.fromRGB(255,0,0)
	end
end)

this is where ive been going crazy, it detects serverevent PERFECTLY FINE but for whatever reason MODE AND COLOR DONT CHANGE AND I CANT FIGURE OUT WHY

WHEN I PRINT MODE AND COLOR AFTER CHANGING THEM, IT SAYS THEYRE CHANGED BUT THEY REALLY ARENT AND IF I PRINT ANYWHERE ELSE IN THE SCRIPT IT ALSO SAYS IT ISNT CHANGED

i would really appreciate the help here because im genuinely losing my mind trying to figure this out ive been at this for 2 entire hours

Where is the mode variable stated? It might not be done correctly, and changing it to

local Mode = "Attack"

Could help. I would recommend also printing out “Mode” at the start of the function to see what happens.

If using Color3 on your basepart doesn’t work, you can also try changing the BrickColor as an alternative to see if that works:

script.Parent.Handle.Color = Color3.fromRGB(0,0,255)
-- turns into:
script.Parent.Handle.BrickColor = BrickColor.new("Really Blue")

May I also add that making a variable for the handle could help make the script more readable, as it is used a couple of times in the function.

Use elseif in the if block, otherwise it just changes it through each option

So I don’t really know how to fix it since I’m not good at scripting tools,
but adding print statement’s to the script might make it easier to find the bugs.

But I wrote a script that will hopefully make it easier to find the bugs or fix the bug overall:
ServerScript:

local Mode = “Attack”

script.Parent.SwordSwitch.OnServerEvent:Connect(function(plr)
print(“received”)
if Mode == “Attack” then
print(“Mode Attack”)
Mode = “Knockback”
script.Parent.Handle.Color = Color3.fromRGB(0,0,255)
elseif Mode == “Knockback” then
print(“Mode Knockback”)
Mode = “Heal”
script.Parent.Handle.Color = Color3.fromRGB(0,255,0)
elseif Mode == “Heal” then
print(“Mode Heal”)
Mode = “Attack”
script.Parent.Handle.Color = Color3.fromRGB(255,0,0)
elseif not Mode then
print(“No Modes”)
end
end)

LocalScript look’s fine to me so I won’t add any changes there.

model boundary is kinda weird so the only way i did was to send the mode and had it checked in OnServerEvent

local UID = game:GetService("UserInputService")
local ToolEquipped = false

script.Parent.Equipped:Connect(function()
	ToolEquipped = true
end)

script.Parent.Unequipped:Connect(function()
	ToolEquipped = false
end)

UID.InputBegan:Connect(function(key, typing)
	if not typing then
		if key.KeyCode == Enum.KeyCode.Q and ToolEquipped then
			script.Parent.SwordSwitch:FireServer("Attack") --send it as a string
		end
	end
end)

--inside server script
Mode = "Attack"

script.Parent.SwordSwitch.OnServerEvent:Connect(function(plr, actionz)
	print("received")
	if actionz == "Attack" then
		Mode = "Knockback"
		script.Parent.Handle.Color = Color3.fromRGB(0,0,255)
	end
	if actionz == "Knockback" then
		Mode = "Heal"
		script.Parent.Handle.Color = Color3.fromRGB(0,255,0)
	end
	if actionz == "Heal" then
		Mode = "Attack"
		script.Parent.Handle.Color = Color3.fromRGB(255,0,0)
	end
end)

problem with this is that it would literally only fire attack

the fact that elseif was all it needed to fix it is insane
jesus i gotta remember that exists

1 Like

it had local on it, i just forgot to put that into the post as it wasnt copy pasted lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.