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

Script adds npcs locally, but why is it also causing the player to fly backwards when they jump?

Asked by 6 years ago

To help alleviate some server strain, I decided to have my random npcs spawn locally on the client instead of the server. This script is a starter player script that randomly chooses an npc and randomly spawns it at one of my set spawn spots. It spawns 30 npcs total. Everything works fine... except when the player jumps, the player character is thrown backwards several yards. Here is the full script:

local npcSpawnEvent = game.ReplicatedStorage:WaitForChild("NpcSpawnEvent")

local player = game.Players.LocalPlayer 
local character = player.Character or player.CharacterAdded:wait()
--Setup bin 
local bin = Instance.new("Message")
bin.Name = 'LocalNPCBin'
bin.Parent = character

--Set Spawn Points
for i = 1, 44 do
    local spawnPart = game.ReplicatedStorage:WaitForChild("SpawnPart"..i):Clone()
    spawnPart.Parent = bin
    spawnPart.Name = "SpawnPart"..i
end

function onNpcSpawnEvent()
    for i = 1, 30 do
        --randomize around the spawning spot so multiple npcs aren't ontop of each other
        local pos1 = math.random(-10,10)
        local pos2 = math.random(-10,10)
        --choose random spawn spot
        local rndSpawn = math.random(1,44)
        --choose random direction for npc to face
        local rndTurn = math.random(-360,360)
        local rndTurn2 = math.random(-360,360)
        --choose random npc
        local rndNum = math.random(1,70)

        if rndNum <= 10 then
            local newNPC = game.ReplicatedStorage.NPC1:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=11 and rndNum <=20 then
            local newNPC = game.ReplicatedStorage.NPC2:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=21 and rndNum <= 30 then
            local newNPC = game.ReplicatedStorage.NPC3:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=31 and rndNum <= 40 then
            local newNPC = game.ReplicatedStorage.NPC4:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=41 and rndNum <= 50 then
            local newNPC = game.ReplicatedStorage.NPC5:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=51 and rndNum <= 60 then
            local newNPC = game.ReplicatedStorage.NPC6:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        elseif rndNum >=61 and rndNum <= 70 then
            local newNPC = game.ReplicatedStorage.NPC7:Clone()
            newNPC:MakeJoints()
            newNPC.Parent = bin
            newNPC.UpperTorso.CFrame = CFrame.new(bin["SpawnPart" .. rndSpawn].Position + Vector3.new(pos1,0,pos2),Vector3.new(0,rndTurn,rndTurn2)) --CFrame.new(Vector3.new(pos1,10,pos2))
            newNPC.Name = "NPC"
        end 

    end
end
npcSpawnEvent.OnClientEvent:Connect(onNpcSpawnEvent)

The issue must be within the function onNpcSpawnEvent, but I can't figure out what exactly is going on.

0
Lowering the number of total npcs to 10 seems to have fixed the jumping issue... but I don't understand why it happens at all. Buggims 4 — 6y

Answer this question