So as you can see from the title, I am trying to make a tool that spawns a part. I have made it so it cant be spammed crashing the server in under a second but it just wont spawn anything . Here u have my code
local JustSpawned = false local Tool = game.StarterPack.SandTool local Player = game:GetService("Players").LocalPlayer; local Mouse = Player:GetMouse(); function draw() local Sand = Instance.new("Part") Sand.Anchored = false Sand.Size = Vector3.new(0.1,0.1,0.1) Sand.BrickColor = BrickColor.new("Sand yellow") JustSpawned = true wait(1) JustSpawned = false end if Tool.Equipped then if JustSpawned == false then Mouse.Button1Down:connect(function() draw() end) end end
I found a few issues:
You didn't parent the Sand
Few errors
A better syntax could be used
Let's start with the first issue: Parenting
You need to parent the Sand to the workspace or a descendant of the workspace in order to make it visible.
It can be garbage-collected if not parented.
Errors
The position wasn't specified
Anything on the StarterPack would get automatically re-parented into the player client's Backpack
Syntax
Tools already has their own function if the mouse clicks while tools are equipped: Activated
if var == false
can be replace with not
task.wait(n)/task.wait() are advised to use than wait(n)/wait(0
Here is the fixed script: (Assuming it's a Client Script)
local JustSpawned = false local Tool = script.Parent local plr = game:GetService("Players").LocalPlayer local Character = plr.Character or plr.CharacterAdded:Wait() local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") -- // This is if it's R15 function draw() local Sand = Instance.new("Part") Sand.Anchored = false Sand.Size = Vector3.new(0.1,0.1,0.1) Sand.Position = Vector3.new(math.random(HumanoidRootPart.Position.X, HumanoidRootPart.Position.X + 20), HumanoidRootPart.Position.Y + 25, math.random(HumanoidRootPart.Position.Z, HumanoidRootPart.Position.Z + 20)) Sand.BrickColor = BrickColor.new("Sand yellow") Sand.Parent = workspace JustSpawned = true task.wait(1) JustSpawned = false end Tool.Activated:Connect(function() if not JustSpawned then draw() end end)