Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why Isn't my GUI button not spawning a part on a position?

Hello! Im making a GUI that spawns a part and the position of the part goes Is basically the player Itself, however the problem Is the part doesn't teleport (Or goes) to the player Itself, this Is the script:



script.Parent.MouseButton1Down:Connect(function(plr)
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(4.01, 5.464, 2.519)
	Part.Parent = game.Workspace
	Part.Position = plr.Character.HumanoidRootPart.Position
end)

1 comment

0

You need to define plr, and you need a remote event so that you can transfer the data to the server

2 answers

✓ Accepted answer

MouseButton1Down does not return a plr. Therefore, you can use game.Players.LocalPlayer

local plr = game.Players.LocalPlayer

Remove 'plr' line 1.

It should be:

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Down:Connect(function()
    local Part = Instance.new("Part")
    Part.Size = Vector3.new(4.01, 5.464, 2.519)
    Part.Parent = game.Workspace
    Part.Position = plr.Character.HumanoidRootPart.Position
end)

You need a remote to do this. Use A RemoteEvent in Replicated Storage, Fire it from your TextButton, and make the actual script in ServerScriptService. Make sure the script in ServerScriptService isnt a localscript.

TextButton Script:


REM = game.ReplicatedStorage:WaitForChild("RemoteEvent")
script.Parent.MouseButton1Down:Connect(function(plr)
	REM:FireServer(REM)
end)

ServerScriptService Script:

REM = game.ReplicatedStorage:WaitForChild("RemoteEvent")
REM.OnServerEvent:Connect(function(plr)
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(4.01, 5.464, 2.519)
	Part.Parent = game.Workspace
	local LP = game.Players.LocalPlayer
	Part.Position = game.Players.LocalPlayer.Position
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.