How to add debounce to mesh cloning script?

I want to clone a star mesh to a part however theres no debounce so after another part is shot out from the weapon it turns back into a normal part with no mesh and the mesh gets sent to the new one and so on so there only one part with the mesh on it.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local bc = BrickColor
local int = BrickColor.new("Dark blue")
local kin = BrickColor.new("Lime green")
local det = BrickColor.new("Really red")
local random = math.random(1,3)
local starmesh = game.ReplicatedStorage.Starmesh:Clone()
local dmg = game.ReplicatedStorage["Damage Script"]:Clone()

script.Parent.Activated:Connect(function(input)
	--create the bullet when we click the mouse button: 		
	local star = Instance.new("Part")
	star.Shape = Enum.PartType.Ball
	star.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-1) -- position the bullet two studs in front of the players character
	star.CFrame = CFrame.new(star.Position, mouse.Hit.p) --make the bullet face the mouse position
	star.Size = Vector3.new(2,2,2)
	star.CanCollide = false
	star.Material = Enum.Material.Neon
	dmg.Parent = star

	--create a bodyforce which combats gravity when shooting
	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * star:GetMass(), 0)
	antiGravity.Parent = star

	star.Parent = workspace

	--fire the bullet
	star.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the star to go slower
end)

This is a local script which clones the mesh from replicatedstorage to the part in the tool I have.

1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local bc = BrickColor
local int = BrickColor.new("Dark blue")
local kin = BrickColor.new("Lime green")
local det = BrickColor.new("Really red")
local random = math.random(1,3)
local starmesh = game.ReplicatedStorage.Starmesh:Clone()
local dmg = game.ReplicatedStorage["Damage Script"]:Clone()
local Debounce = false

script.Parent.Activated:Connect(function(input)
if Debounce == false then
    Debounce = true
	--create the bullet when we click the mouse button: 		
	local star = Instance.new("Part")
	star.Shape = Enum.PartType.Ball
	star.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-1) -- position the bullet two studs in front of the players character
	star.CFrame = CFrame.new(star.Position, mouse.Hit.p) --make the bullet face the mouse position
	star.Size = Vector3.new(2,2,2)
	star.CanCollide = false
	star.Material = Enum.Material.Neon
	dmg.Parent = star

	--create a bodyforce which combats gravity when shooting
	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * star:GetMass(), 0)
	antiGravity.Parent = star

	star.Parent = workspace

	--fire the bullet
	star.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the star to go slower
    task.wait(3)
    Debounce = false
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local bc = BrickColor
local int = BrickColor.new("Dark blue")
local kin = BrickColor.new("Lime green")
local det = BrickColor.new("Really red")
local random = math.random(1,3)
local starmesh = game.ReplicatedStorage.Starmesh:Clone()
local dmg = game.ReplicatedStorage["Damage Script"]:Clone()
local db = false

script.Parent.Activated:Connect(function(input)
	--create the bullet when we click the mouse button: 
    if db == false then
       db = true
        local star = Instance.new("Part")
	star.Shape = Enum.PartType.Ball
	star.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-1) -- position the bullet two studs in front of the players character
	star.CFrame = CFrame.new(star.Position, mouse.Hit.p) --make the bullet face the mouse position
	star.Size = Vector3.new(2,2,2)
	star.CanCollide = false
	star.Material = Enum.Material.Neon
	dmg.Parent = star

	--create a bodyforce which combats gravity when shooting
	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * star:GetMass(), 0)
	antiGravity.Parent = star

	star.Parent = workspace

	--fire the bullet
	star.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the star to go slower
        task.wait(3)
        db = false
     end		
end)

If you’re trying to replicate something to server and want others to see it, use a remote, if not , this script is okay.

Thanks for this!, I’ve tried to replicate it to a serverscript by remote but I dont know how fully?
My server script is this :

local tool = script.Parent
local event = tool:WaitForChild('Event')
event.OnServerEvent:Connect(function(player, mouseEvent)
	print(player, mouseEvent)
	--create the star when we click the mouse button: 		
	local star = Instance.new("Part")
	local starmesh = game.ReplicatedStorage.Starmesh:Clone()
	local dmg = game.ReplicatedStorage["Damage Script"]:Clone()

	star.Shape = Enum.PartType.Ball
	star.BrickColor = BrickColor.new("Black")
	star.Anchored = false
	star.CanCollide = false
	star.Massless = true
	star.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-1)
	star.CFrame = CFrame.new(star.Position, mouse.Hit.p) --make the star face the mouse position
	star.Size = Vector3.new(2,2,2)
	dmg.Parent = star
	starmesh.Parent = star
	--gravity is a nono
	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * star:GetMass(), 0)
	antiGravity.Parent = star

	--add star to workspace before firing
	star.Parent = workspace
	star.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the star to go slower
	mouse = mouseEvent
end)

Do you know how I could fix it or anything?

Tip:

When using FireServer, InvokeServer , there is no need to provide the player as the first argument, as it already does that for you, which means:
If we have something like this:
remote:FireServer(Arg1,Arg2)
On the server it’d be:
remote.OnServerEvent(Player,Arg1,Arg2)

Also notice,
You can’t access the mouse from the server,
but you can pass the mouse’s target[object] or hit[cframe] within the remote.

Like this?

local player = game.Players.LocalPlayer
local tool = script.Parent
local event = tool:WaitForChild('Event')
event.OnServerEvent:Connect(function(player, mouseEvent)
	print(player, mouseEvent)
	--create the star when we click the mouse button: 		
	local star = Instance.new("Part")
	local starmesh = game.ReplicatedStorage.Starmesh:Clone()
	local dmg = game.ReplicatedStorage["Damage Script"]:Clone()

	star.Shape = Enum.PartType.Ball
	star.BrickColor = BrickColor.new("Black")
	star.Anchored = false
	star.CanCollide = false
	star.Massless = true
	star.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-1)
	star.CFrame = CFrame.new(star.Position, mouse.Hit) --make the star face the mouse position
	star.Size = Vector3.new(2,2,2)
	dmg.Parent = star
	starmesh.Parent = star
	--gravity is a nono
	local antiGravity = Instance.new("BodyForce")
	antiGravity.Force = Vector3.new(0, workspace.Gravity * star:GetMass(), 0)
	antiGravity.Parent = star

	--add star to workspace before firing
	star.Parent = workspace
	star.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the star to go slower
	mouse = mouseEvent
end)

^ Thats my server script, This is my localscript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent

local event = tool:WaitForChild('Event')

event:FireServer(mouse.Hit)

However it doesnt say anything and outputs

attempt to index nil with 'Hit'