Why does this not work?

Hello!

So I am making a shop where you can buy items ad all that, and one of the items is DoubleJump. Whenever you buy it, it enables the jump script in StarterCharacterScripts. However, the double jump does not work when bought. I tried enabling it work when the player joined, and that worked.
Shop script:

local DoubleJumpScript = game.StarterPlayer.StarterCharacterScripts:WaitForChild("DoubleJumpScript")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local leaderstats = Player:WaitForChild("leaderstats")
local Coins = leaderstats:WaitForChild("Coins")

script.Parent.MouseButton1Click:Connect(function()
	if Coins.Value == 1000 or Coins.Value >= 1000 then
		Coins.Value -= 1000
		DoubleJumpScript.Enabled = true
		print("script enabled")
	else
		if Coins.Value <= 1000 then
			print("not enough coins")
			script.Parent.Parent.Parent.Parent.Error:Play()
		end
	end
end)

Jump script:

local MaxJumps = 2
local JumpCooldown = 0.2
----------------------------------------- Settings

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local NumJumps = 0
local canjump = false

Humanoid.StateChanged:Connect(function(oldstate, newstate)
	if Enum.HumanoidStateType.Landed == newstate then
		NumJumps = 0
		canjump = false
	elseif Enum.HumanoidStateType.Freefall == newstate then
		wait(JumpCooldown)
		canjump = true
	elseif Enum.HumanoidStateType.Jumping == newstate then
		canjump = false
		NumJumps += 1
	end
end)

UIS.JumpRequest:Connect(function()
	if canjump and NumJumps < MaxJumps then
		Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end)

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

This is because you are referencing the unloaded script in StarterCharacterScripts and not the one loaded in your player which would be found in PlayerScripts. Simply replace where you find the script with game.Players.LocalPlayer.PlayerScripts:WaitForChild(‘DoubleJumpScript’)

2 Likes

You’re correct but that would be the case for StarterPlayerScripts. This is the character scripts so it would be stored inside the players character.

1 Like

Oh dang you right, forgot it wasn’t StarterPlayerScripts. Yeah, just try to find the script within the player’s character and enabling it. Thanks for pointing that out.

2 Likes

It’s all good! You had the right idea, just a minor error.

1 Like

It should be this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local DoubleJumpScript = Character:WaitForChild("DoubleJumpScript")

local leaderstats = Player:WaitForChild("leaderstats")
local Coins = leaderstats:WaitForChild("Coins")

script.Parent.MouseButton1Click:Connect(function()
	if Coins.Value == 1000 or Coins.Value >= 1000 then
		Coins.Value -= 1000
		DoubleJumpScript.Enabled = true
		print("script enabled")
	else
		if Coins.Value <= 1000 then
			print("not enough coins")
			script.Parent.Parent.Parent.Parent.Error:Play()
		end
	end
end)
1 Like

It worked! Thank you so much! Have a great day!

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