Why won't my code work

my code just doesn’t work. I have is so that when I press a button it should do an attack. I copied the code over from another game I’m working on and it works fine there, it just breaks when I try it in my new game.

heres my code

uis.InputBegan:Connect(function(I, E)
	if E then return end
	if I.UserInputType == Enum.UserInputType.Keyboard then
		if I.KeyCode == Enum.KeyCode.One then
			if plr.Character.Action.Value == true then return end
			if plr.Character.Blocking.Value == true then return end
			if canmove1 == false then return end
			if ismoves1 == true then
				canmove1 = false
				move1:Play()
				spawn(function()
					wait(plr.PlayerGui.Main.Moveset.Attack1.Cool.Value)
					plr.PlayerGui.Main.Moveset.Attack1.Flash.Transparency = 0.5
					canmove1 = true
					for i = 0, 10 do
						wait(0)
						plr.PlayerGui.Main.Moveset.Attack1.Flash.Transparency = plr.PlayerGui.Main.Moveset.Attack1.Flash.Transparency + 0.05
					end
				end)
				plr.PlayerGui.Main.Moveset.Attack1.Cooldown.Size = UDim2.new(0, 57, 0, -57)
				plr.PlayerGui.Main.Moveset.Attack1.Cooldown:TweenSize(UDim2.new(0, 57, 0, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, plr.PlayerGui.Main.Moveset.Attack1.Cool.Value, true, function()

				end)
			end
		end
    end
end
1 Like

I’m sorry, but respectfully, … this code is a mess.
Clean up your script before coming here, so we have an easier time helping you. Here are the style (and some performance) issues that I’ve noticed:

  1. Variable names should be longer than a character
    Make variable names clear, so that in a long function, you do not have to scroll up to see what I means.

  2. Stay consistent
    You use blocks but also early returns, you use just the variable name, but also comparison. Pick one, and stay with it.

  3. Use not instead of equality operators.

  4. Use task.wait instead of wait

  5. Use tweenservice instead of tweensize

  6. Use variables to avoid repeating code.

local tweenService = game:GetService("TweenService")

uis.InputBegan:Connect(function(inputObject: InputObject, gameProcessed: boolean) --use context action service, or the new inputaction system
    if gameProcessed then return end
    if inputObject.UserInputType ~= Enum.UserInputType.Keyboard then return end --check redunant, one cannot be typed from xbox or mobile.
    if inputObject.KeyCode == Enum.KeyCode.One then return end

    local character = plr.Character -- use variables, not only more readable, but also more performant
    if character.Action.Value or character.Blocking.Value then return end --group related statements
    if not canmove1 or not ismoves1 then return end --use logical operations instead of '=='
    canmove1 = false

    move1:Play()
    local movesetUI = plr.PlayerGui.Main.Moveset.Attack1

    task.wait(movesetUI.Cool.Value) --use task.wait instead of wait
    canmove1 = true
 
    movesetUI.Flash.Transparency = 0.5
    tweenService:Create(movesetUI.Flash, TweenInfo.new(0.1),  {["Transparency"] = 1}):Play()

    movesetUI.Cooldown.Size = UDim2.fromOffset(57, -57) --this doesn't scale on other screens. 
    tweenService:Create(movesetUI.Cooldown, TweenInfo.new(movesetUI.Cool.Value), {["Size"] = UDim2.fromOffset(57, 57)}):Play() --use tweenservice instead of tweensize.
end)

Sources:
TweenService | Documentation - Roblox Creator Hub
InputContext | Documentation - Roblox Creator Hub
ContextActionService | Documentation - Roblox Creator Hub

Regarding actually solving the issue, are there any errors in the console? What exactly isn’t working? Are all required components present in the new project? Move this topic to Help and Feedback > Scripting Support

5 Likes

not very helpfull. I think i can code how i want. im asking if there is a reason why code i copied over from another game completely suddenly doesn’t work.

1 Like

If code of another game is different and requires it’s own game resources than yours other game resources.

This could be the issue

1 Like

did you even read my last sentence? The 3 or so questions regarding the actual issue? Is there anything in the console? Maybe some missing instances that were present in the previous project?

2 Likes

if you think you can code how you want then also go ahead and fix it yourself since youre such a good programmer. If its code you copied then youll have to fix it for your game. Stop asking questions on the devforum and refusing to take advice from programmers obviously way more experienced than you

4 Likes

even if it may be unhelpful it is feedback he is just trying to help. You never had to code his way but I do believe it would improve your scripting by being a bit more organized.

1 Like

Could you show more of the script(s) and I also don’t know what you mean by “break” is it stopping at a certain part is it returning before the rest of the scripts run? are you getting a error?

From just a quick glance, it looks like it would work. Do you have canmove initialized as true initially, and ismoves1 initalized as a thing?

1: (Add prints to see if a value is set to true for any of your return ends)
2: (ismoves1/canmove1 are not initalized initially in the code snippet)

im trying to take advice, its just that I have everything from the previous game copied over to the new game. it works fine for every other script, its just that one script. if it was someting i forgot to copy over from the previous one or im missing something in the script usually it would give me an error. but it doesn’t give me an error and it just doesn’t do anything, most of the code inside the button press trigger should be fine, it just doesn’t trigger when i press the button. idk if i just didn’t explain it well in the first place

1: I’ll try that
2: they are initialized earlier in the script and should work fine.

nevermind i think i might have found the problem, the variable ismoves1 (which checks if you have 1 or more moves) doesn’t flip to true, maybe because it checks before you get the moves. imma try to fix this and check back.

It worked I fixed it. the check just happens too soon. I added a wait and it works fine

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