Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Spawn Model on Touch?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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

0
stop begging raid6n 2196 — 4y
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. GreatTurkeySandwich 2 — 4y
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 Ziffixture 6913 — 4y
Ad

Answer this question