I have a horse model but I am not sure how to make it so the player clicks on the ground which is like within 12 studs of the player then a horse spawns. Do you guys know how I can fix do this?
if you know how to use GetMouse(), use that and if the target is Terrain, spawn the horse there. If you mostly want to make it so if it is 12 studs away, use magnitude. LMK if you don’t know what either of the things I mentioned were.
You can use ProximityPrompt.
Here is a example code how you can do it:
local Players = game:GetService("Players")
-- Define the distance within which the player can spawn a horse
local maxSpawnDistance = 12
-- Create a ProximityPrompt that will spawn the horse when triggered
local horsePrompt = Instance.new("ProximityPrompt")
horsePrompt.ActionText = "Spawn Horse"
horsePrompt.MaxActivationDistance = maxSpawnDistance
horsePrompt.Parent = Your ground location here
-- Listen for when the ProximityPrompt is triggered
horsePrompt.Triggered:Connect(function(player)
-- Spawn the horse model (change this part to your horse model)
local horse = Instance.new("Part")
horse.Name = "Horse"
horse.CanCollide = false
-- Set the horse's position to the player's position
horse.CFrame = player.Character.HumanoidRootPart.CFrame
-- Set the horse's parent to workspace
horse.Parent = workspace
end)
I put this in the tools local script right?
Its a script not local script and you put this script in “ground”.
But I want to tool to spawn the horse. Like for example you click the ground the horse gets spawned
I’m having difficulty with that part.
Oh, now i understand your problem. One moment
Okay, thank you for the help bro.
Sorry for a late reply i dint got time yesterday, but here is a example (you can start with) to spawn a horse:
LocalScript in Tool
-- Define the tool
local tool = script.Parent
-- Enable the tool when equipped and disable it when unequipped
function onEquipped()
tool.Enabled = true
end
function onUnEquipped()
tool.Enabled = false
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnEquipped)
-- Set up the mouse events to spawn the horse when the left mouse button is pressed
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
function onLeftMouseButtonClick()
if not tool.Enabled then
return
end
-- Get the mouse's current position
local x, y, z = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
-- Clone the horse model if it exists in the workspace, and set its position to the mouse's current position
local horse = workspace:FindFirstChild("Horse")
if horse then
local copy = horse:Clone()
copy.Name = "Horse"
copy:PivotTo(CFrame.new(x, y, z))
copy.Parent = workspace
print("Horse spawned!")
else
warn("Horse model not found!")
end
end
mouse.Button1Down:Connect(onLeftMouseButtonClick)