[SOLVED] problem with neck break code

Eh-Oh!

I was trying to do a script to when you’re walking, if you’re with bad luck, you slip and break ur neck.

Here is my script:

-->variables


local humanoid = script.Parent:WaitForChild('Humanoid')
local humanoid_root_part = script.Parent:WaitForChild('HumanoidRootPart')


local neck_break_sound = Instance.new('Sound')


local random = math.random(0, 1)


-->settings


neck_break_sound.SoundId = 'rbxassetid://1093102664'
neck_break_sound.Parent = humanoid_root_part


-->script


while wait() do


	if humanoid:ChangeState() == Enum.HumanoidStateType.Running then
		
		
		if random == 1 then
			
			
			humanoid.Health = 0
			neck_break_sound:Play()
			
			
		end	
		
		
	end
		
		
end

And no, there’s nothing wrong when i press F9.

|||||[ Images ]|||||

|||||[ Thanks For Helping, TinkyWinkyDev. ]|||||

In your loop, random doesn’t get updated - so once it is decided at the start of the script, it wont change. Your loop also has no wait time, so it is running every tick, which could become problematic.

Also, you need a remote event to fire if you are doing this through a localscript. see this post for more details

Hope this helps!


while wait(someamountoftime) do -- put a time to wait here so it isn't checking every tick

    local random = math.random(0, 1) -- maybe change 1 to a higher number to lower chances depending on how it plays out
	if humanoid:ChangeState() == Enum.HumanoidStateType.Running then
    
		if random == 1 then
			
			neck_break_sound:Play() -- play the sound first in case sound bugs out on death (i dont do much work with sound so i woudln't know)
			[[fire remote event here]]
			
			
		end	
		
	end	

end
1 Like

man this is a dark script if i ever seen one

1 Like

I know, its because it will be a fighting game.

Thanks, you helped me a lot, my script was not working.

No problem! Good luck with your project!

1 Like

lol i srsly thought it was some kind of troll game

1 Like