Issue with an AI script

Wanted to do the particle appear when double jumped, and i cant double jump somewhy

local plr = game.Players.LocalPlayer.Character
local humanoid = plr.Humanoid
local doubleJumped = false
local particle

function onJump()
	if not (humanoid:GetState() == Enum.HumanoidStateType.Jumping) then
		doubleJumped = false
	end
end

function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if not doubleJumped and humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
			doubleJumped = true
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

			-- Создание и настройка Particle
			particle = Instance.new("ParticleEmitter")
			particle.Parent = plr.Character.HumanoidRootPart
			if plr.doubleJumped then
				particle.Enabled = true
			else 
				wait(1)
				particle.Enabled = false
			end

			-- Добавьте анимацию при двойном прыжке
			local animTrack = humanoid:LoadAnimation(script.Parent.DoubleJumpAnimation)
			animTrack:Play()
		end
	end
end

function onCharacterAdded(character)
	humanoid = character:WaitForChild("Humanoid")
	humanoid.Jumping:Connect(onJump)
end

plr.CharacterAdded:Connect(onCharacterAdded)
game:GetService("UserInputService").InputBegan:Connect(onKeyPress) 

Please someone help
When pressed space 2 times, its not working. I even spammed it. Still not working.

6 Likes

These conflict each other, it’s rather confusing to read the code
Anyways try this:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() -- get character
local humanoid = char.Humanoid
local doubleJumped = false
local particle

function onJump()
	if not (humanoid:GetState() == Enum.HumanoidStateType.Jumping) then
		doubleJumped = false
	end
end

function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if not doubleJumped and humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
			doubleJumped = true
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

			-- Создание и настройка Particle
			particle = Instance.new("ParticleEmitter")
			particle.Parent = plr.Character.HumanoidRootPart
			if doubleJumped then -- what is plr.doubleJumped?????
				particle.Enabled = true
			else 
				wait(1)
				particle.Enabled = false
			end

			-- Добавьте анимацию при двойном прыжке
			local animTrack = humanoid:LoadAnimation(script.Parent.DoubleJumpAnimation)
			animTrack:Play()
		end
	end
end

function onCharacterAdded(character)
	humanoid = character:WaitForChild("Humanoid")
	humanoid.Jumping:Connect(onJump)
end

onCharacterAdded(char) -- you forgot to fire function for the first character

plr.CharacterAdded:Connect(onCharacterAdded)
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
3 Likes

Bro You need to learn some scripting before you depend on using AI
And don’t get embarrassed by this or something instead You can get Angry And use that anger to Motivate you to learn more scripting. Good Luck
but I’ll try to fix your code

  1. plr is initialized as the character, which might not work as expected. You should get the player character only when a character is added.

  2. plr.doubleJumped is being checked, but it’s not a valid property. You should check doubleJumped instead of plr.doubleJumped.

  3. The code for enabling and disabling the particle emitter is inside the if plr.doubleJumped block. You should place this logic outside the if plr.doubleJumped condition.

Here’s a better version of your code (plus i haven’t checked this if it works in roblox studio this is guess work)

local player = game.Players.LocalPlayer
local character
local humanoid
local doubleJumped = false
local particle

function onJump()
    if not (humanoid:GetState() == Enum.HumanoidStateType.Jumping) then
        doubleJumped = false
    end
end

function onKeyPress(input)
    if input.KeyCode == Enum.KeyCode.Space then
        if not doubleJumped and humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
            doubleJumped = true
            humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

            -- Create and configure Particle
            particle = Instance.new("ParticleEmitter")
            particle.Parent = character:WaitForChild("HumanoidRootPart")
            particle.Enabled = true

            -- Disable particle after a while (adjust the wait time as needed)
            wait(1)
            particle.Enabled = false

            -- Add animation for double jump
            local animTrack = humanoid:LoadAnimation(script.Parent.DoubleJumpAnimation)
            animTrack:Play()
        end
    end
end

function onCharacterAdded(character)
    character:WaitForChild("Humanoid").Jumping:Connect(onJump)
    character:WaitForChild("HumanoidRootPart")
end

-- Character added event
player.CharacterAdded:Connect(function(char)
    character = char
    humanoid = character:WaitForChild("Humanoid")
    onCharacterAdded(character)
end)

-- Input service event
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
7 Likes

Not gonna lie, RadAnimeBro is right.

Before using AI, try learning to code yourself.

AI can be wrong, ya know.

:exploding_head:

I know.

So learn to code at least a bit so that if it doesn’t work, you have a 80% chance of being able to fix it.

Fortunately, chatGPT is correct most of the time…

But even chatGPT makes mistakes

:exploding_head: :exploding_head: :exploding_head:

plr.doubleJumped is not a propert of the player or the character, therefore you will get an error. To check if they double jump try see if they hit the spacebar/jumping key two times in quick succession.

1 Like

Actually it works, but i added some fixes:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local doubleJumped = false


function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if doubleJumped == false and humanoid:GetState() == Enum.HumanoidStateType.Freefall or humanoid:GetState() == Enum.HumanoidStateType.Jumping then
			doubleJumped = true
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

			-- Create and configure Particle
			local particle = Instance.new("ParticleEmitter")
			particle.Enabled = true
			particle.Parent = character:WaitForChild("HumanoidRootPart")
local animTrack = humanoid:LoadAnimation(script.Parent.DoubleJumpAnimation)
            animTrack:Play()
			-- Disable particle after a while (adjust the wait time as needed)
			repeat task.wait() until (humanoid:GetState() == Enum.HumanoidStateType.Landed)
			particle.Enabled = false
			particle:Destroy()
doubleJumped = false
		end
	end
end

-- Input service event
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
2 Likes

It works but animation is playing late + i cant sometimes double jump because of “stack end” thing in output

1 Like

Try now, i changed code a little bit.

1 Like

not working, cant double jump
(char limit)

Are you creating a new particle emitter every time? And I don’t see it being destroyed either so even if it worked the more the player double jumps the more particle emitters there are, they probably will not harm performance but you can just make a particle emitter when the character is added

Why would you write script with NN in first place? I don’t know why scripting is too hard to learn for many. Also ChatGPT doesn’t work 100% times at scripting you know. So in this situation learning scripting is actually better than wonder why ChatGPT scripts doesn’t work and ask it on forum where you will get answer with delay (sometimes massive).
Just advice.

Thats what i am talking about.

do you think im able to model, translate and script at the same time

No, but if you can’t script you don’t need to do so. Specially, if you don’t want to learn the basics. You must understand that ChatGPT can’t do all work for you. Even to correct script you need to know basics of scripting.

Do what you want to do and currently learning

want to do scripting
currently learning modeling

Learn how to script then i guess…