Search code examples
luaroblox

How can I make a model get spawned right next to me in roblox?


I am trying to make a dummy spawner for my gun testing game but I cant get it to spawn next to the player

I tried to use this but it just gives me an error saying "attempt to index number with 'Character'"

script.Parent.MouseButton1Down:Connect(function(player)
    local char = player.Character or player.CharacterAdded:Wait()
    local pos = char:GetPrimaryPartCFrame().p
    local clone = script.Parent.Dummy:Clone()
    clone.Parent = game.Workspace
    clone:MoveTo(pos)
end)

Solution

  • .MouseButton1Down event returns the X and Y screen coordinate in pixels, not the player as specified in the api reference

    thus doing 5.Character causes an error

    now for your problem

    assuming this is a localscript

    local player = game.Players.LocalPlayer
    
    script.Parent.MouseButton1Down:Connect(function()
        local char = player.Character or player.CharacterAdded:Wait()
        local pos = char:GetPrimaryPartCFrame().p
        local clone = script.Parent.Dummy:Clone()
        clone.Parent = game.Workspace
        clone:MoveTo(pos)
    end)