[SOLVED] Problems with giving perks (like speed boosts and jump boosts) to a player

Hello i have got a problem where my code works sometimes. However it doesn’t give the perks sometimes (which means it didn’t work, but no errors occur) and only rejoining can fix that. It is very annoying, and I was wondering, if there is any possible fix for my script without re-writing literally everything.

P.S: I would like to know better ways of giving perks to the player, because i am sure if mine not the worst one, it is definitely not the best one.

Thanks for any help!

Additional info: It’s a normal script in the starter gui (because i do not know how to make it work with a local script); Classes are leaderstats string values, which i store by using data store services later.

Code:

local local_player = script.Parent.Parent
local character = local_player.Character
local humanoid = character:WaitForChild("Humanoid")
local leaderstats = local_player:WaitForChild("leaderstats")
local rs = game:GetService("ReplicatedStorage")
local class = leaderstats:WaitForChild("Class")
local tools = rs:WaitForChild("to_save_tools")


repeat task.wait(0.05) until local_player

print("loaded")
class.Changed:Connect(function()
	local normal_speed = 14
	local normal_jump = 7.2
	local normal_hp = 100
	print("changed")
	local cash_multiplayer = local_player:WaitForChild("CashMultiplayer")
	local success, errormsg = pcall(function()
	if class.Value == "Owner" then
		--health boost
		humanoid.MaxHealth = normal_hp * 1.8
		humanoid.Health = normal_hp * 1.8
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed * 2.4
		humanoid.JumpHeight = normal_jump * 1.8
		--special-moves
		cash_multiplayer.Value = 3
		for i, v in pairs(tools:GetChildren()) do
			local cloned_tool = v:Clone()
			cloned_tool.Parent = local_player.StarterGear
		end
		for i, v in pairs(tools:GetChildren()) do
			local cloned_tool = v:Clone()
			cloned_tool.Parent = local_player.Backpack
		end
		elseif class.Value == "OVERWRITE-USERS" then
		--health boost
		humanoid.MaxHealth = normal_hp * 1.5
		humanoid.Health = normal_hp * 1.5
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed * 2.2
		humanoid.JumpHeight = normal_jump * 1.6
		--special-moves
		cash_multiplayer.Value = 2.5
		for i, v in pairs(tools:GetChildren()) do
			local cloned_tool = v:Clone()
			cloned_tool.Parent = local_player.StarterGear
		end
		for i, v in pairs(tools:GetChildren()) do
			local cloned_tool = v:Clone()
			cloned_tool.Parent = local_player.Backpack
		end
		elseif class.Value == "Noobs" then
		--health boost
		print("changed")
		humanoid.MaxHealth = normal_hp * 1.15
		humanoid.Health = normal_hp * 1.1
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed * 1.1
		humanoid.JumpHeight = normal_jump * 1.2
		--special-moves
		for i, v in pairs(local_player.Backpack:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		for i, v in pairs(local_player.StarterGear:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		elseif class.Value == "Snas" then
		--health boost
		humanoid.MaxHealth = normal_hp * 1.3
		humanoid.Health = normal_hp * 1.3
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed * 2.1
		humanoid.JumpHeight = normal_jump * 1.6
		
		cash_multiplayer.Value = 1.7
		--special-moves
		for i, v in pairs(local_player.Backpack:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		for i, v in pairs(local_player.StarterGear:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
	elseif class.Value == "Pro" then
		--health boost
		humanoid.MaxHealth = normal_hp * 1.6
		humanoid.Health = normal_hp * 1.6
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed * 1.8
		humanoid.JumpHeight = normal_jump * 1.8
		
		
		cash_multiplayer.Value = 1.4
		--special-moves
		for i, v in pairs(local_player.Backpack:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		for i, v in pairs(local_player.StarterGear:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		elseif class.Value == "None" then
		--health boost
		humanoid.MaxHealth = normal_hp
		humanoid.Health = normal_hp
		--jumpboosts & speedboosts
		humanoid.WalkSpeed = normal_speed
		humanoid.JumpHeight = normal_jump
		
		cash_multiplayer.Value = 1
		--special-moves
		for i, v in pairs(local_player.Backpack:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
				end
		end
		for i, v in pairs(local_player.StarterGear:GetChildren()) do
			if v then
				local cloned_tool = v
				cloned_tool:Destroy()
			end
		end
		end

	end)	
end)

2 Likes

Where have you placed this script?
Is it inside a tool?
Also, I think this might be the problem:

for i, v in pairs(local_player.StarterGear:GetChildren()) do
	if v then
		local cloned_tool = v
		cloned_tool:Destroy()
	end
end

If it is inside a tool then maybe your deleting it is causing it not to work more than once.

1 Like

it is inside the starter gui, and it is a normal script. I did that loop so other classes won’t get items higher ones get

1 Like

how is a local player a script.Parent.Parent shouldnt you just do game:GetService(“Players”).LocalPlayer

1 Like

Is that possible to do game:GetService(“Players”).LocalPlayer when it’s a normal script?
i thought it was not possible

script itself works, but sometimes it doesn’t detect if the string value was changed. That’s the problem i think, and i have no idea how to fix it

I thought its a local script plus u cant really get local player on a server side script. To get the player u need to do :Connect(function(plr)

Btw shouldnt you do these boosters in a local script

i do not think .Changed will work in a local script, or it will?

Yes it should work in a local script. Its even better tbh to do it in a local

I’ll transfer whole code in local script. However as i told before there is no 100% chance for it to detect if class value was changed, sometimes it detects it, sometimes no

and im trying to figure out how to fix that, so there is always 100% chance for it to work

just solved it myself, and it works with local script now. Thanks to everyone who tried to help me!