Log in to vote
0

# [Solved]Why when I clone this part the code doesn't work inside of it?

Asked by 2 days ago
Edited 1 day ago

So basically what I want to do is to make a script that will duplicate a part all around my map. But the problem is that the part duplicates but the script inside the part doesn't work.

Edit1: Main Script is located in workspace

Main Script to duplicate parts

while wait(0.1) do
local bullet = game.Workspace.Bullet:Clone()
local jump = game.Workspace.Jump:Clone()
local invisible = game.Workspace.Invisible:Clone()
local health = game.Workspace.Medkit:Clone()
local speed = game.Workspace.Speed:Clone()
bullet.Parent = workspace
bullet.Position = Vector3.new(math.random(-205, 186), 20, math.random(-132, 262))
end


Script inside the part named bullet

Edit1: The script is located in a part that is in workspace

local part = script.Parent
local DoubbleeBulletGamepass = true
game.Players.PlayerAdded:Connect(function(player)
local maxBullet = player:WaitForChild("leaderstats"):WaitForChild("MaxBullet")
local bulletLeft = player:WaitForChild("leaderstats"):WaitForChild("BulletLeft")
local hitName = nil
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hitName = hit.Parent.Name
if bulletLeft.Value < maxBullet.Value then
if DoubbleeBulletGamepass == true and bulletLeft.Value <= 4 then
bulletLeft.Value = bulletLeft.Value + 2
elseif DoubbleeBulletGamepass == true and bulletLeft.Value < maxBullet.Value then
bulletLeft.Value = bulletLeft.Value + 1
elseif DoubbleeBulletGamepass == false and bulletLeft.Value < maxBullet.Value then
bulletLeft.Value = bulletLeft.Value + 1
end
part:Destroy()
elseif bulletLeft.Value >= maxBullet.Value then
print("you have max ammo")
end
end
end)
end)


I have tried to put the main script variables to be from replicated storage but it still doesn't work(Also if you can could you please explain what you did)

### 1 answer

Log in to vote
0
Answered by 2 days ago

There is no point in even creating a new script instance every single time. You can just hook it up to when you create the part.

Another thing you did wrong is that you are using the PlayerAdded event to get the player, however this is only fired when a player joins the game.

Don't worry though! We can easily find the player through the character. I will display how in the script below.

Another thing that I will add is that collisions are very easily spammable. We are going to fix this by disconnecting the Touched event.

I commented the script I wrote for you a fair amount but do let me know if you have any questions.

One thing that I am worried about is the amount of bullets you are creating. 10 bullets per second can become very laggy very quickly. Consider upping the time delay. Up to you though.

Like so,

while wait(0.1) do
local bullet = game.Workspace.Bullet:Clone()
local jump = game.Workspace.Jump:Clone()
local invisible = game.Workspace.Invisible:Clone()
local health = game.Workspace.Medkit:Clone()
local speed = game.Workspace.Speed:Clone()
bullet.Parent = workspace
bullet.Position = Vector3.new(math.random(-205, 186), 20, math.random(-132, 262))

-- Let's make a variable for a connection so that we can access it later
local connection
connection = bullet.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
-- sometimes it may touch a hat, for example.

if humanoid then
-- Okay so now we know whatever hit the bullet has a humanoid
-- Let's make sure that it's a player

local char = humanoid.Parent
local hitName = char.Name

local player = game:GetService("Players"):GetPlayerFromCharacter(char)

local DoubbleeBulletGamepass = true
-- DoubbleeBulletGamepass is kind of unnecessary, but
-- I am guessing it will have use in future so I'll keep it here for you

if player then
if typeof(connection) == "RBXScriptConnection" then
-- Could == nil and error so let's avoid that.
-- This will avoid collision spam so that we don't get too many bullets added
connection:Disconnect()
end
local bulletLeft = player:WaitForChild("leaderstats"):WaitForChild("BulletLeft")
local maxBullet = player:WaitForChild("leaderstats"):WaitForChild("MaxBullet")

--// Bullet addition logic
if bulletLeft.Value < maxBullet.Value then

if DoubbleeBulletGamepass == true and bulletLeft.Value <= 4 then
bulletLeft.Value = bulletLeft.Value + 2

elseif DoubbleeBulletGamepass == true and bulletLeft.Value < maxBullet.Value then
bulletLeft.Value = bulletLeft.Value + 1

elseif DoubbleeBulletGamepass == false and bulletLeft.Value < maxBullet.Value then
bulletLeft.Value = bulletLeft.Value + 1

end
bullet:Destroy()

elseif bulletLeft.Value >= maxBullet.Value then
print("you have max ammo")
end
end
end
end)
end


Also, this script will now work and you can delete whatever script is inside of Bullet.

Please accept my response if I have answered your question :)

0
thank you for the explanation PurpleSouley1234 19 — 1d