Hello! So I am trying to it make to where when you equip a certain tool, you will be able to place a block that explodes whenever the mouse clicks. It works, but I get the error in the title.
Here is my code (local script:)
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ExplosionBlock = ReplicatedStorage:WaitForChild("ExplosionBlock")
local tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local MousePos = UserInputService:GetMouseLocation()
Mouse.Button1Down:Connect(function()
ExplosionBlock.Parent = game.Workspace
ExplosionBlock.Position = MousePos
end)
Code in a regular script parented to the block in RS:
local Sound = script.Parent:WaitForChild("Sound")
while true do
task.wait(1)
script.Parent.BrickColor = BrickColor.random()
end
local function explode()
while true do
task.wait(1)
Sound:Play()
end
local Explosion = Instance.new("Explosion")
Explosion.Parent = game.Workspace
Explosion.Position = script.Parent.Position
end
explode()
I think I could maybe optimize it by using RemoteEvents or something.
If you think you know what went wrong, please let me know. Thank you and have a wonderful day!
It’s because you’re cloning the part on the client side, but that block script is server side. You’re gonna have to use a RemoteEvent, or do all the effects on the client.
If I did everything on the client, would it look like this?
--Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--Variables
local tool = script.Parent
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local ExplosionBlock = ReplicatedStorage:WaitForChild("ExplosionBlock")
local Sound = ExplosionBlock:WaitForChild("Sound")
--Functions
Mouse.Button1Down:Connect(function()
local newBlock = ExplosionBlock:Clone()
newBlock.Position = Mouse.Hit.Position
newBlock.Parent = workspace
end)
while task.wait(1) do
ExplosionBlock.BrickColor = BrickColor.random()
Sound:Play()
end
local function explode()
local Explosion = Instance.new("Explosion")
Explosion.Parent = game.Workspace
Explosion.Position = ExplosionBlock.Position
end
explode()
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local tool = script.Parent
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local ExplosionBlock = ReplicatedStorage:WaitForChild("ExplosionBlock")
--//Functions
Mouse.Button1Down:Connect(function()
local newBlock = ExplosionBlock:Clone()
newBlock.Position = Mouse.Hit.Position
newBlock.BrickColor = BrickColor.random()
newBlock.Parent = workspace
task.spawn(function()
while task.wait(1) and newBlock:IsDescendantOf(workspace) do
newBlock.BrickColor = BrickColor.random()
end
end)
local Explosion = Instance.new("Explosion")
Explosion.Position = newBlock.Position
Explosion.Parent = workspace
newBlock.Sound:Play()
end)
--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local tool = script.Parent
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local ExplosionBlock = ReplicatedStorage:WaitForChild("ExplosionBlock")
--//Functions
Mouse.Button1Down:Connect(function()
local newBlock = ExplosionBlock:Clone()
newBlock.Position = Mouse.Hit.Position
newBlock.BrickColor = BrickColor.random()
newBlock.Parent = workspace
for i = 1, 5 do
newBlock.Sound:Play()
newBlock.BrickColor = BrickColor.random()
task.wait(1)
end
local Explosion = Instance.new("Explosion")
Explosion.Position = newBlock.Position
Explosion.Parent = workspace
newBlock:Destroy()
end)