Trying to spawn van at humanoidrootpart when the player clicks textbutton

hello! im trying to clone a van from replicatedstorage and pivot it to the humanoidrootpart when the player clicked the text button. i cant seem to get it to work tho. it works when i pivot it to normal parts but i want to clone it to the character. ty :slight_smile:

script.Parent.Visible = true

local button = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)

local function SpawnVan()
	local van = game.ReplicatedStorage:WaitForChild("One Seated Van")


			local vanClone = van:Clone()
			vanClone.Parent = workspace
			vanClone:PivotTo(char.HumanoidRootPart.CFrame)


end
		button.MouseButton1Click:Connect(SpawnVan)

	end)
end)

Have you used print to check whether the event actually runs in the first place?

hello dan! tysm 4 ur reply.

i really shouldve added a print statement. mb. i modified the script but im still not getting anything unfortunately.

updated:

script.Parent.Visible = true

local button = script.Parent


local function SpawnVan(char)
	button.MouseButton1Click:Connect(function()
	local van = game.ReplicatedStorage:WaitForChild("One Seated Van")

print("spawn function works")
			local vanClone = van:Clone()
			vanClone.Parent = workspace
			vanClone:PivotTo(char.HumanoidRootPart.CFrame)
print("working")
	end)
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		SpawnVan(char)
	end)
end)

Hi there, so firstly you want to make sure that your script is a LocalScript, this is for the GUI and button clicking only. Before you proceed to the following steps please add a RemoteEvent and name it “SpawnVan” inside of ReplicatedStorage. Once you do that, just update your Local Script in your GUI or wherever it is to this:

script.Parent.Visible = true

local button = script.Parent

button.MouseButton1Click:Connect(function()
	game.ReplicatedStorage:WaitForChild("SpawnVan"):FireServer()
end)

After this, insert a regular script into ServerScriptService and enter this code inside:

local SpawnVan = game.ReplicatedStorage:WaitForChild("SpawnVan")

SpawnVan.OnServerEvent:Connect(function(Player : Player)
	local char = Player.Character
	if char == nil then return end
	local van = game.ReplicatedStorage:WaitForChild("One Seated Van")
	print("spawn function works")
	local vanClone = van:Clone()
	vanClone.Parent = workspace

	vanClone:PivotTo(char.HumanoidRootPart.CFrame)
end)

Hopefully this works! Let me know if there are any errors or anything else I can help with.

1 Like

u absolutely legend. i shouldve used a remote event UGH i didnt think of it. tysm man i really appreciate ur help! have a great day :slight_smile:

full scripts:
localscript:

local event = game.ReplicatedStorage:WaitForChild("SpawnVan")
local button = script.Parent

button.Visible = true

button.MouseButton1Click:Connect(function()
	event:FireServer()
end)

script:

local event = game.ReplicatedStorage:WaitForChild("SpawnVan")
local button = script.Parent

event.OnServerEvent:Connect(function(plr: Player)
	local char = plr.Character
	if char == nil then return end
	local van = game.ReplicatedStorage:WaitForChild("One Seated Van")
	print("spawn func works")
	local vanClone = van:Clone()
	local SpawnOFFSET = CFrame.new(0,20,0)
	vanClone.Parent = workspace
	vanClone:PivotTo(char.HumanoidRootPart.CFrame * SpawnOFFSET)
end)
1 Like

Happy to help, good luck on your game! :smile:

1 Like

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