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
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:
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.
Stay consistent
You use blocks but also early returns, you use just the variable name, but also comparison. Pick one, and stay with it.
Use not instead of equality operators.
Use task.wait instead of wait
Use tweenservice instead of tweensize
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)
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
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.
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?
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
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.
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?
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
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.