Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there to simplify this script?

Okay i'd say i'm a 2/10 scripter and i've been working on this on and off and I feel like i'm doing something wrong. It's supposed to be a Run script, if you hold shift the bar goes down..you release it charges. Simple right? After I urged myself to not go to this site to ask for help for the past 2 days on previous versions of this script I finally gave in. 1 You can exploit the charge by spamming shift and 2 I feel like it could be simplified If anyone can help and 3 It seems the health script (that's in the game) works fine on multiplayer but this run script only works instudio, can someone explain that too I'm really confused please click on my name and go to my profile I made the game uncopylocked so you can look at the script in depth if you have too

MyMouse = game.Players.localPlayer:GetMouse()
Player = game.Players.localPlayer
local xSize = script.Value.Value
local speed = 0.01
local human = script.Parent.Parent.Parent.Parent.Character.Humanoid
--local human = Player.Character:FindFirstChild("Humanoid")
--script.Parent.Parent.Parent.Parent.Character.Humanoid

MyMouse.KeyDown:connect(function(key)
	if (key:byte() == 48) then
		while script.Bool.Value == true do
			if xSize > 0.01 then
				script.Bool.Value = true
				human.WalkSpeed = 100
				for i = 1,1 do
					xSize = xSize - speed
					wait()
					script.Parent.Bar.Size = UDim2.new(xSize,0,1,0)
				end
			end
			if xSize <= 0.01 then
				human.WalkSpeed = 16
				script.Bool.Value = false
			end
		end
	end
end)

MyMouse.KeyUp:connect(function(key)
	if (key:byte() == 48) then
		human.WalkSpeed = 16
		script.Bool.Value = false
		while script.Bool.Value == false do
			if xSize < 1 then
				for i = 1,1 do
					xSize = xSize + speed
					wait()
					script.Parent.Bar.Size = UDim2.new(xSize,0,1,0)
				end
			else
				wait()
				script.Bool.Value = true
			end
		end
	end
end)

1 comment

0

Is the key "48" the left shift key? Also, I believe Lua is case-sensitive, and you have "localPlayer" instead of "LocalPlayer". Is there anything in your output?

1 answer

✓ Accepted answer

Actually, the issues here are minimal, good job! First of all, I don't know if this could be simplified... I know of a couple of different ways it could be written, but I wouldn't say they are simpler or better, just different.

To fix this, there's two things you need to do.

Fix the Exploit:

What's happening is when the player presses the key again, the script starts another while loop which increases the speed at which it recharges. All you need to do, is put the entire Key up function inside a checker so it can't start if it's already started.

Fix the server not working:

Even easier. The problem is the script loads before the other things needed load; specifically bool. You need to do WaitForChild at the beginning so the script doesn't start until bool loads.

I applied these changes; here's the result. (I also made a couple of other changes to help the efficiency of the script; I hope that's ok. I believe it works the way you intended. If you want to force the player to wait for it to recharge each time, uncomment the marked lines; also, you don't really need the NumberValue called Value)

script:WaitForChild("Bool")

MyMouse = game.Players.LocalPlayer:GetMouse()
Player = game.Players.LocalPlayer

local xSize = 1
local speed = 0.01
local human = Player.Character.Humanoid
--local human = Player.Character:FindFirstChild("Humanoid")
--script.Parent.Parent.Parent.Parent.Character.Humanoid

MyMouse.KeyDown:connect(function(key)
	if (key:byte() == 48) then
		script.Bool.Value = true
				
		--if xSize ~= 1 then
		--	script.Bool.Value = false--------Uncomment this section to force the player to recharge fully before use again
		--end
		
		while script.Bool.Value == true do
			if xSize > 0.01 then
				human.WalkSpeed = 100
				xSize = xSize - speed
				wait()
				script.Parent.Bar.Size = UDim2.new(xSize,0,1,0)
			end
			if xSize <= 0.01 then
				human.WalkSpeed = 16
				break
			end
		end
	end
end)

MyMouse.KeyUp:connect(function(key)
	if script.Bool.Value == true then
		if (key:byte() == 48) then
			human.WalkSpeed = 16
			script.Bool.Value = false
			while script.Bool.Value == false do
				if xSize < 1 then
					xSize = xSize + speed
					wait()
					script.Parent.Bar.Size = UDim2.new(xSize,0,1,0)
				else
					wait()
					break
				end
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.