I am making a game which involves key binds. I have four scripts located in player scripts which each do different key binds: Sprint is double tapping "w", glide is double taping space, super jump is holding and releasing "t", and night vision is holding "q". All these scripts should work the same, and they do... in studio, but once you enter the game, things start getting weird.

MY SCRIPTS:

Sprint:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local max = 10
local stamina = 30
local canrun = false
local runing = false
mouse.KeyDown:connect(function(key)
	if key == "w" then --Select a button, if you want shift then put 48 in it
		if canrun and stamina > max/3 then
			wait(0.1)
		
			humanoid.WalkSpeed = 30
			canrun = false
			runing = true
		else
			wait(0.1)
		
		 canrun = true
		
		wait (0.5) canrun = false
		end
	end
end)

mouse.KeyUp:connect(function(key)
	if key =="w" then

		humanoid.WalkSpeed = 16
		runing = false
	end

end)

while true do wait(1)
	if runing then 
		stamina = stamina - 1
		if stamina < 0 then 
			humanoid.WalkSpeed = 16
			runing = false
			stamina = 0
		end
		
	end
	if runing == false then
		stamina = stamina + 1
		if stamina > max then 
			stamina = max
		end
	end
end

this script acts strange:

Glide:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local max = 50
local stamina = 50
local canrun = false
local runing = false
mouse.KeyDown:connect(function(key)
	if key == " " then --Select a button, if you want shift then put 48 in it
		if canrun and stamina > max/3 then
			wait(0.1)
			print ("running") 
			canrun = false
			runing = true
		else
			wait(0.1)
			print ("moving")
	 	canrun = true
	
		wait (0.5) canrun = false
		end
	end
end)

mouse.KeyUp:connect(function(key)
	if key ==" " then
		print ("walking")
		runing = false
	end

end)

while true do wait(0.05)
	if runing then 
		print (stamina)
		stamina = stamina - 1
		power = stamina
		if power > 25 then
			else power = power*2
		end
		humanoid.Parent.Head.Velocity = humanoid.Parent.Head.Velocity * Vector3.new(1,0,1)
		if stamina < 0 then 
			runing = false
			stamina = 0
		end
	
	end
	if runing == false then
		stamina = stamina + 1
		if stamina > max then 
			stamina = max
		end
	end
end

this script acts strange as well:

Super Jump:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local max = 5
local stamina = 30
local charge = false
local power = 0
mouse.KeyDown:connect(function(key)	
if key == "t" then --Select a button, if yo	u want shift then put 48 in it
		wait(0.1)	
		print ("charging")
		
		 charge = true
	end
end)

mouse.KeyUp:connect(function(key)
	if key =="t" then
		print ("walking")
		humanoid.Sit = false
		wait(0.2)	
		humanoid.JumpPower = power*20
		power = 0
		humanoid.Jump = true
	charge = false
	wait(1)
	humanoid.JumpPower = 50
	humanoid.WalkSpeed = 16
	
	end

end)

while true do wait(1)
	if charge then 
		humanoid.Sit = true
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
			stamina = stamina - 1
		power = power + 2
	print (power)
			if stamina < 0 then 
			humanoid.Sit = false
			wait(0.2)
			humanoid.JumpPower = 20*power
			power = 0
			stamina = 0
			humanoid.Jump = true
			charge = false
			wait(1)
			humanoid.JumpPower = 50
			humanoid.WalkSpeed = 16
			
		end
		
	end
	if charge == false then
		stamina = stamina + 1
		if stamina > max then 
			stamina = max
		end
	end
end

Final Script, this one appears to work fine:

Night Vision: (for seeing in caves, the only script which only modifies local things, the shortest script)

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local max = 10
local stamina = 30
local canrun = true
local runing = false
mouse.KeyDown:connect(function(key)
	if key == "q" then --Select a button, if you want shift then put 48 in it
		if stamina > max/3 and canrun then
			print ("on") 
			game.Lighting.GlobalShadows = false
			canrun = false
			runing = true

		
		end
	end
end)

mouse.KeyUp:connect(function(key)
	if key =="q" and runing then
		print ("off")
		canrun = true
		game.Lighting.GlobalShadows = true
		runing = false
	end

end)

Observations on how scrips behave: all scripts run normally when only one player is in game

when two players join the game both players start with most scripts running normally (I have had it to where one player's glide script appears to be broken)

once a player dies or resets, he usually re spawns with only night vision working. The next player who dies usually has the key binds working from then on it appears to be random, usually all key binds working or only night vision, which consistently works just fine.

I am REALLY confused. I have absolutely no idea why this happens or how to fix it. All four of these scripts are located in starter player scripts. there are no errors in studio

please help!

3 comments

0

KeyDown is deprecated, do NOT use it in new work, and do not post free models.

0

I wasn't posting a free model... and this work is only sort of new.. but I will fix it... looks like there isn't an answer

0

wow I figured this out a few days ago :D just had to put :WaitForChild in front