The image below shows a script being made, right now it is REALLY confusing if you are just looking at it right now.
What I’m trying to do: I’m trying to make it so when I touch a Part it will create a new part within ‘Workspace’ as you can see below, that’s what I’m trying to do. But, I do not know what to do next, I have tried many ways, but I do not seem to understand it right now.
Please explain to me what I need to do next. If it is confusing still, please let me know so I can edit this post.
What you’ve done should work fine. Although I would recommend adding a debounce to avoid rapid spam. I’d also recommend setting your variable to nil when you’re done with it in order to clear the reference in memory to allow for garbage collection. Lastly, you should use either lowercase workspace(as it’s a global variable), or use game:GetService('Workspace'). GetService() should be used when referencing any service. Even if the names are unchanged, it’s good practice to do so.
Make sure to check in the explorer if the parts are being added and where. Perhaps it’s not in a place that’s visible to you.
You’re spawning all of the parts to the same potion and they’re anchored, so you won’t see it spawning more unless you look in the explorer. Also is the button anchored? If not, it may be triggering itself.
add a parameter in the function as hit and check if it has a humanoid. It’s running because another part is already touching it (like the baseplate)
workspace.button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local newPart = Instance.new("Part")
newPart.Reflectance = 1
newPart.Anchored = true
newPart.Material = Enum.Material.Neon
newPart.Color = Color3.fromRGB(255,0,0)
newPart.Position = Vector3.new(36.844,0.5,25.238)
newPart.Parent = game.Workspace
end
end)
If you want a debounce
local db = true
workspace.button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not db then
db = true
local newPart = Instance.new("Part")
newPart.Reflectance = 1
newPart.Anchored = true
newPart.Material = Enum.Material.Neon
newPart.Color = Color3.fromRGB(255,0,0)
newPart.Position = Vector3.new(36.844,0.5,25.238)
newPart.Parent = game.Workspace
end
task.wait(1) --how long you want it to wait
db = false
end
end)
Every Service is an object. Not using GetService is bad practice. It’s just more convenient it use lowercase workspace as it’s an already pre-defined global variable for Workspace. Meaning that there’s no reason, and it’s better not, to use game.Workspace over just workspace.
Sure, it might not need to change, just as you can still use wait() when task.wait() exists. However, task.wait() should be used instead preferably.
_
It’s important to use existing globals or service calls as they’ll work no matter if the services have been renamed or not. They also create the service if it doesn’t exist already, because not all services exist by default. I imagine this will be even more common in the future.
Now it prints the name of the object, because it exists after being called in.
Obviously the Workspace will probably always be loaded by default, along with the rest of the commonly used services that are required for a game to function, but it’s still bad practice to not reference them. Names could change, they could stop existing by default, or there’s just a better and faster option already (such as lowercase workspace).