Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
Thank you! i diddnt realise that but thanks! ahsan666666666666 264 — 4y
0
np! Noonekirby 162 — 4y
Ad

Answer this question