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

if part clicked then clone itemFrame to Inventory?

Asked by 5 years ago
Edited 5 years ago

Hello So im having a problem with an Cloning ItemFrame to Inv Gui The thing i wanna do is Mouse1 clicked on the part then it clones the ItemFrame {ImageLabe} to inventory Gui So i can see it. script

local player = game:GetService("Players").LocalPlayer

local ui = game.ReplicatedStorage.ItemFrame.BottleOfMilk

local newui = ui:Clone()

function onClicked(player)

newui.Parent = player.PlayerGui.Main.Inv.inv

end

script.Parent.ClickDetector.MouseClick:Connect(onClicked) why it does not work?

0
Server Script* User#21499 0 — 5y
0
You never defined the parameter that MouseClick passes. DeceptiveCaster 3761 — 5y
0
That's why you're getting the error. DeceptiveCaster 3761 — 5y
0
McAnd So i fixed it its same it does not clone the Frame User#21499 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

There are two things wrong here:

  1. You're accessing the PlayerGui via the server. Unless your game is in Experimental Mode, you won't be able to pull this off.
  2. There is no variable that defines player. This should be the player who clicked the ClickDetector as a parameter in the function onClicked.

One solution is to relocate the script to StarterGui and make it a local script. That would look like this:

local ui = game:GetService("ReplicatedStorage").ItemFrame.BottleOfMilk
local newui = ui:Clone()
local function onClicked(player)
    newui.Parent = player.PlayerGui.Main.Inv.inv
end
workspace.Part.ClickDetector.MouseClick:Connect(onClicked)

The other solution is to use RemoteEvents like so.

Server Script in the Part containing the ClickDetector:

local event = game:GetService("ReplicatedStorage"):WaitForChild("UIEvent")
script.Parent.ClickDetector.MouseClick:Connect(function()
    event:FireClient()
end)

Local Script in StarterGui:

local event = game:GetService("ReplicatedStorage"):WaitForChild("UIEvent")
local ui = game:GetService("ReplicatedStorage").ItemFrame.BottleOfMilk
local newui = ui:Clone()
event.OnClientEvent:Connect(function(player)
    newui.Parent = player.PlayerGui.Main.Inv.inv
end)
0
The server can't access the descendants of PlayerGui but it can very much add instances to it, there is no need of an event Ankur_007 290 — 5y
0
Are you blind? DeceptiveCaster 3761 — 5y
0
Also, it is highly recommended to use RemoteEvents for this sort of thing. Not sure where you're pulling your false claims out from. DeceptiveCaster 3761 — 5y
0
Ohh, I see Ankur_007 290 — 5y
0
Sorry for the problems Ankur_007 290 — 5y
Ad

Answer this question