I'm trying to make a part spawn in a model from ServerStorage when touched. I can't figure out what I'm doing wrong, please excuse my ignorance.
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then function SpawnModel() model = game.ServerStorage.Model:clone() model.Parent = game.Workspace model:MoveTo(spawn.Position) end end)
Thanks.
EDIT: Nevermind, I fiddled around and I eventually got it to work with this:
script.Parent.Touched:connect (function() function SpawnModel() model = game.ServerStorage.Model:Clone() model.Parent = workspace end SpawnModel() end)
This is because you've wrapped all of the code responsible for placing the model in workspace
into the scope of a function, anything within a function will not run unless it's called, to call a function, you write the function handle with parentheses:
local Players = game:GetService("Players") script.Parent.Touched:Connect(function(hit) -- Connect should be capitalized if (Players:GetPlayerFromCharacter(hit.Parent)) then function SpawnModel() model = game.ServerStorage.Model:Clone() -- This too model.Parent = workspace -- workspace can be referenced without game model:MoveTo(spawn.Position) end SpawnModel() end)
However, this approach isn't the best method of achieving your goal, another form with a functional approach should be used instead:
function onTouched() -- Content end script.Parent.Touched:Connect(onTouched)
Hope this helped, if so, don't forget to accept and upvote