Hello I’m new to raycasting and I was wondering how can I spawn parts after the character resets or die there is no error in the code. I tried using position and cframe none worked.
StarterCharacterScripts:
local character = script.Parent
local Humanoid = character:FindFirstChildWhichIsA("Humanoid")
Humanoid.Died:Connect(function()
local humanoidRootPart = character.HumanoidRootPart.CFrame
for i = 1, 50 do
local part = Instance.new("Part")
part.Size = Vector3.new(math.random(1, 6), math.random(1, 6), 0.05)
part.Transparency = 0
part.CanCollide = false
part.Anchored = true
local rayOrigin = humanoidRootPart.Position
local rayDirection = Vector3.new(math.random(-50, 50),math.random(-50, 50), math.random(-50, 50)).Unit * 3
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {character}
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.IgnoreWater = true
local result = workspace:Raycast(rayOrigin, rayDirection, rayParams)
if result then
part.CFrame = CFrame.new(result)
part.Parent = workspace
end
game:GetService("Debris"):AddItem(part, 20)
end
end)
What exactly are you trying to achieve? I understand you want to spawn parts after the character dies, but is there any specific pattern in which you want them to spawn?
I was able to get a working script here in which rays fire out in a sphere around the player upon death, however, I don’t know if it’s what you want:
local Debris = game:GetService("Debris")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {char}
rayParams.IgnoreWater = true
local r = Random.new()
hum.Died:Connect(function()
for i = 1, 50 do
local rayDirection = r:NextUnitVector() * 10
local rayOrigin = root.Position
local result = workspace:Raycast(rayOrigin, rayDirection, rayParams)
if result then
local part = Instance.new("Part")
part.Size = Vector3.new(r:NextInteger(1,6), r:NextInteger(1,6), 0.05)
part.CanCollide = false
part.Anchored = true
local pos = result.Position
part.Position = pos
part.Parent = workspace
Debris:AddItem(part, 20)
end
end
end)
Edit: it should be stated that due to the recursive nature of this function, the parts created will collide with each other. If this is not desired you should create a table and insert each new part into it, then update the filter accordingly