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

why does my game keep crashing when i play this script?

so ive been working on this script that when a key is pressed it fires some functions to open a aura type thing around the player and it checks the magnitude around the player for other players. if a player is in certain distance from the local player it fires a function to damage them. but when i test or play the game and test it with a alt account eh game crashes. heres the script:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:wait()
local db = true
local onn = false

local Distance = 15


game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.Z and db then
		db = false
		if onn == false then
			onn = true
		elseif onn == true then
			onn = false
		end
		for _,Plr in pairs(Players:GetPlayers()) do
			if Plr ~= Player then
			    local Chr = Plr.Character
				local HRP = Chr:WaitForChild("HumanoidRootPart")
				repeat
					if HRP and Player:DistanceFromCharacter(HRP.CFrame.Position) < Distance then
						local MYHRP = script.Parent.HumanoidRootPart.CFrame.Position
						local mag = (HRP - MYHRP).Magnitude
						if mag < Distance then
							script.Parent.FireKillingIntent:FireServer(HRP)
							wait(0.5)
						end
			        	print("In Range")
					end
				until onn == false
			end
		end
		wait(0.5)
		db = true
	end
end)



game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.Z and db then
		db = false
		if onn == false then
			onn = true
		elseif onn == true then
			onn = false
		end
		script.Parent.OpenkillingIntent:FireServer()
		local san = game.Workspace.SAN.HumanoidRootPart
		repeat
			local HRP = Char.HumanoidRootPart.CFrame.Position
			local mag = (san.CFrame.Position - HRP).Magnitude
			if mag < Distance then
				script.Parent.FireKillingIntent:FireServer(san)
				wait(0.5)
			end
		until onn == false
		wait(0.5)
		db = true
	end
end)

the first UserInputService function is for players and the second is for a npc in workspace i used for testing. i cant even get which line im crashing on because when the game crashes i have no choice but to leave. anyway i hope somone can help me, and thank you!

1 answer

✓ Accepted answer

it's likely the repeat, as it only waits under certain conditions, and not always. for example,


repeat
                    if HRP and Player:DistanceFromCharacter(HRP.CFrame.Position) < Distance then
                        local MYHRP = script.Parent.HumanoidRootPart.CFrame.Position
                        local mag = (HRP - MYHRP).Magnitude
                        if mag < Distance then
                            script.Parent.FireKillingIntent:FireServer(HRP)
                            wait(0.5)
                        end
                        print("In Range")
                    end
                until onn == false

only waits under multiple if statements, meaning if mag was greater than distance, it would run without waiting, causing the script to crash.This happens on the other repeat as well.

2 comments

0

Thank you! i diddnt realise that but thanks!

0

np!

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