Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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)
0
You need to define plr, and you need a remote event so that you can transfer the data to the server Frometa1998 35 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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)
Ad
Log in to vote
0
Answered by 3 years ago

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)

Answer this question