Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Spawn Model on Touch?

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)

1 answer

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

3 comments

0

stop begging

0

Thanks for your help, but that hasn't gotten it to work correctly either. I can get it to work with a click detector for some reason, but not on touch.

0

Well, I'm keen in fixing this issue since I've already tried, so please, tell me what you can figure out about what's not working

This discussion is preserved as it was. New answers, comments, and votes are closed.