Is their any possible way to position a part after a GUI button click to the player who has pressed the GUI?
local Player = [Player].Character
[GUI Button location].Mousebutton1click:Connect(function()
[Part].Position = Vector3.new(Player.LeftFoot.Position)
[Part].Position = [Part].Position + Vector3.new(3, 0, 0)
end)
Yes you can place the part after clicking on the GUI button. It is enough for the code with part placement to be in the local script. Only player, which clicked you Gui button will see how changes.
Below I present you a sample code.
Be sure this localScript is in your ScreenGui.
local ScreenGui = script.Parent
local TextButton = ScreenGui:WaitForChild("TextButton")
local Part = game.ReplicatedStorage.Part -- Part to clone or part to move
local cframe = CFrame.new(0, 2, 0) -- Your cframe
local interaction = false
TextButton.MouseButton1Click:Connect(function()
if not interaction then
interaction = true
local clone = Part:Clone()
clone.Parent = workspace
if clone:IsA("Part") then
clone.CFrame = cframe
elseif clone:IsA("Model") then
clone:PivotTo(cframe)
end
end
end)
For the player I want it as whoever clicks the button howdo u do that
For the player I want it as whoever clicks the button how do u do that
So you want, when any player to click on the Gui button, everyone will see a part?
So we can do it this way:
-
Add RemoteEvent to ReplicatedStorage.
-
Modify my previous code. (localScript)
Local script code.
local ScreenGui = script.Parent -- Your screenGui
local TextButton = ScreenGui:WaitForChild("TextButton") -- Your textButton, which will be clicked
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Your remoteEvent
TextButton.MouseButton1Click:Connect(function()
RemoteEvent:FireServer()
end)
-- LocalScript in your ScreenGui
- Add the server script to ServerScriptService.
Server script code.
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- Your remoteEvent
local interaction = false
local Part = game.ReplicatedStorage.Part -- Your part to clone/move
local cframe = CFrame.new(0, 2, 0) -- your cframe
RemoteEvent.OnServerEvent:Connect(function(player)
if not interaction then
interaction = true
local clone = Part:Clone()
clone.Parent = workspace
if clone:IsA("Part") then
clone.CFrame = cframe
elseif clone:IsA("Model") then
clone:PivotTo(cframe)
end
end
end)
-- Script in ServerScriptService
I hope this solve your problem!
I mean whats the code to get the player that has clicked the button
local ScreenGui = script.Parent -- your screenGui
local TextButton = ScreenGui:WaitForChild("TextButton") -- your textButton
TextButton.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
print(player)
end)
Oh, yeah so you need to use a LocalScript.
You can get the player with:
local Player = game:GetService(“Players”).LocalPlayer
local part = workspace:WaitForChild("Part")
local button = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
button.MouseButton1Click:Connect(function()
part.CFrame = hrp.CFrame
end)
Are you referring to a Gui button? If so the above will work if the script is a local script inside the button itself.
script.Parent.MouseButton1Click:Connect(function()
game.Workspace.GammaB:Play()
game.Workspace.GammaB2:Play()
game.Workspace.Gamma.GammaBeam.Enabled = true
game.Workspace.Gamma.GammaBeam1.Enabled = true
game.Workpace.Gamma.Position = Vector3.new(Player.LeftFoot.Position)
game.Workpace.Gamma.Position = game.Workspace.Gamma.Position + Vector3.new(3, 0, 0)
wait(5)
game.Workspace.GammaB1:Play()
wait(27)
game.Workspace.Gamma.GammaBeam.Enabled = false
game.Workspace.Gamma.GammaBeam1.Enabled = false
game.Workspace.GammaB:Stop()()
game.Workspace.GammaB2:Stop()()
game.Workspace.GammaB1:Stop()()
end)
The part doesn’t position at the front of the player but it only appears at where it is in the workspace and plays the script
Move the block in relation to the player’s character.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Part = game.Workspace.Part -- Your part to move/clone
Part.CFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -5)