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)