Spawn model script for textbutton [help]

hello, I currently have low script knowledge and would like to get help with this.
basically I need a script for a textbutton that once clicked on, will spawn a model called horse (near the player) and that model is sitting in serverstorage. thanks in advance

2 Likes

Place this inside of the button you want the player to click

--Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local spawnEvent = ReplicatedStorage:WaitForChild("SpawnHorseEvent")

script.Parent.MouseButton1Click:Connect(function()
	spawnEvent:FireServer() -- Notify the server to spawn the horse
end)

add this in your server script service

local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create a RemoteEvent to handle spawning
local spawnEvent = Instance.new("RemoteEvent")
spawnEvent.Name = "SpawnHorseEvent"
spawnEvent.Parent = ReplicatedStorage

-- Listen for the event
spawnEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	if not character then return end

	local rootPart = character:FindFirstChild("HumanoidRootPart")
	if not rootPart then return end

	-- Get the horse model from ServerStorage
	local horseModel = ServerStorage:FindFirstChild("Horse")
	if horseModel then
		local clonedHorse = horseModel:Clone()
		clonedHorse.Parent = workspace -- Move it to the game world
		clonedHorse:SetPrimaryPartCFrame(rootPart.CFrame * CFrame.new(5, 0, 0)) -- Position near the player
	else
		warn("Horse model not found in ServerStorage.")
	end
end)

Please make sure the Horse model does have a primary part set or this will not work from my understanding.

2 Likes

The function CFrame.new(5, 0, 0) creates a CFrame (Coordinate Frame) that represents a position in 3D space. Specifically:

  • 5: The X-coordinate of the position.
  • 0: The Y-coordinate (vertical position).
  • 0: The Z-coordinate (depth).

Since you said you are new to scripting you may want to understand how this works so i am going to leave it here as an addition to your request.

In short this will determine where the model is placed.

2 Likes

thanks so much for the fast reply, it indeed worked

1 Like

image

This is where primary part is located on the properties

2 Likes

If you make any more post and need help and no one responding, Please feel free to DM me. This way i should get the notification and see that you do need help.

2 Likes

yeah indeed, I got a new post, it’s about gamepass locked textbutton

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.