I’m trying to use LineForce to send an object to the player once an image button on a surface GUI is pressed and I wasn’t able to get past the part where I set the LineForce attachment for the player and kept running into errors like this:
local imageButton = script.Parent
local plr = game.Players.LocalPlayer
local function onPressed()
local shrekc = game.ReplicatedStorage.Shrek:Clone()
shrekc.Parent = game.Workspace
local torso = plr.Character.FindFirstChild("UpperTorso")
local att = Instance.new("Attachment")
att.Parent = torso
shrekc.LineForce.Attachment1 = att
end
imageButton.MouseButton1Down:Connect(onPressed)
I’ve already looked at the documentation and on here and just couldn’t really find anything specific enough to my situation.
Thanks!
You seem to be using a Script. Only LocalScripts can access Players.LocalPlayer. I would recommend using a LocalScript in StarterPlayerScripts to detect clicks of the ImageButton.
I’m taking a guess this is a local script, try this:
local imageButton = script.Parent
local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local function onPressed()
local shrekc = game.ReplicatedStorage.Shrek:Clone()
shrekc.Parent = game.Workspace
local char = plr.Character or plr.CharacterAdded:Wait()
local torso = char:WaitForChild("UpperTorso")
local att = Instance.new("Attachment")
att.Parent = torso
shrekc.LineForce.Attachment1 = att
end
imageButton.MouseButton1Down:Connect(onPressed)
Thank you for this, I was able to get it working now, I think some of the main issues I had were actually that the script wasn’t a local script and also the magnitude of the LineForce wasn’t high enough.
Anyways here is my final script located in StarterPlayerScripts if anyone else ever runs into the same problem or whatever. (Also I set LineForce magnitude to 10000):
local imageButton = game.Workspace.Billboard_ui.SurfaceGui.Frame.ImageButton --Finding the image button in workspace since I moved this to starterplayer
local plr = game.Players.LocalPlayer --or game.Players.PlayerAdded:Wait()
local function onPressed()
local shrekc = game.ReplicatedStorage.Shrek:Clone()
shrekc.Parent = game.Workspace
local char = plr.Character --or plr.CharacterAdded:Wait()
local torso = char:WaitForChild("UpperTorso")
local att = Instance.new("Attachment")
att.Parent = torso
shrekc.LineForce.Attachment1 = att
end
imageButton.MouseButton1Down:Connect(onPressed)