Clone random parts

Basicly I am trying to clone random ParticlesEmitters from ReplicatedStorage into the “spray” I tried to make that but I am getting some erros with that. (when the spray(part) touch something one random ParticlesEmitter will be cloned into the spray.)

Image: image
Issue:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("SprayShootEvent")

RemoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)

	local spray = ReplicatedStorage.Paint.Spray:Clone()
	spray.Parent = game.Workspace
	spray.Position = gunPos
	
	local colors = ReplicatedStorage.Paint.Colors
	local colorsChildrens = colors:GetChildren()
	local colorRandomClone = math.random(#1, colorsChildrens)
	
	local cloneColors = colorRandomClone:Clone()
	cloneColors.Parent = spray
	cloneColors.Position = spray.Position
	

	local distance = (mosPos - gunPos).magnitude
	local speed = 200
	spray.CFrame = CFrame.new(gunPos, mosPos)
	spray.Velocity = spray.CFrame.lookVector * speed
	
	local fly = Instance.new("BodyForce",spray)
	fly.Force = Vector3.new(0, spray:GetMass() * workspace.Gravity, 0)
	spray.Orientation = gunOr + Vector3.new(0, -180, 0)	
	
	local attacker = Instance.new("StringValue")
	attacker.Name = "Attacker"
	attacker.Parent = spray
	attacker.Value = player.Name
	
	local touchConnection = nil

	touchConnection = spray.Touched:Connect(function()
		fly.Force = Vector3.new(0, 0, 0)
		touchConnection:Disconnect()
	end)
end)

You’re trying to get the length of a number here. Instead search through the children for a random index.
Simple fix: local colorRandomClone = colorsChildrens[math.random(1, #colorsChildrens)]

2 Likes

yes it works but I am still geting one error in line 12

What’s the error that you’re getting now? Is it the same error?

The colorRandomClone variable is wrong. The # symbol is used to get the length of a table or string, but you’re using it on a number, which is incorrect. You also aren’t indexing the ‘childrens’ table to find this random spray.
Your fixed variable should be:
local colorRandomClone = colorChildrens[math.random(1, #colorsChildrens)]

1 Like

I have the damage script and this script inside the ServerScriptService

ServerScriptService:

local debounce = false

local spray = script.Parent

local function player_check(otherPart)
	if not otherPart.Parent:FindFirstChild("Humanoid") then return end

	local humanoid = otherPart.Parent.Humanoid
	local character = humanoid.Parent
	local humanoidRoot = character.HumanoidRootPart
	
	if humanoid.Parent.Name ~= spray.Attacker.Value then	 
		if otherPart.Name == "Head" then
			humanoid:TakeDamage(35)
		else
			humanoid:TakeDamage(20)
		end
	end
	debounce = false
end

spray.Touched:Connect(player_check)
game.Debris:AddItem(spray, 3)

I used the value with player name for not damage the tool user

This simply just means that ‘Attacker’ does not exist. Try using spray:FindFirstChild(“Attacker”) instead.
Example:

local function player_check(otherPart)
	if not otherPart.Parent:FindFirstChild("Humanoid") then return end
    local Attacker = spray:FindFirstChild("Attacker")

    if Attacker then
		local humanoid = otherPart.Parent.Humanoid
		local character = humanoid.Parent
		local humanoidRoot = character.HumanoidRootPart
	
		if humanoid.Parent.Name ~= Attacker.Value then	 
			if otherPart.Name == "Head" then
				humanoid:TakeDamage(35)
			else
				humanoid:TakeDamage(20)
			end
		end
		debounce = false
    end
end
1 Like

Ok that fixed too and about position issue? What I am doing wrong?

ParticleEmitters don’t have a Position property.

oh ok so I only have to change the parent?

I’d believe so, unless you need to position a part differently.

Not it’s not. It does exist. Indexing like so:
A.B —> script thinks that B is a children of A
Attacker is a string name. Indexing string requires using the square brackets or other means such as FindFirstChild, WaitForChild, etc.

Yes I fixed I changed the Parent only

Thank you @ZuIu_B and @Xx_FROSTBITExX for help :slight_smile:

1 Like

It does not exist. Attacker is the name of an Instance, and it wasn’t found.


If Attacker is a valid Instance:
image
If it is an invalid instance:
image

This is the same with square brackets too.
If Attacker is a valid Instance:
image
If it is an invalid Instance:
image

If one of our answers has solved your issue, you should mark a solution to make sure people know that this has been solved for future reference.

Yes I will mark it :slight_smile:

1 Like