There are two things wrong here:
- You're accessing the
PlayerGui via the server. Unless your game is in Experimental Mode, you won't be able to pull this off.
- 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)
4 comments
Server Script*
You never defined the parameter that MouseClick passes.
That's why you're getting the error.
McAnd So i fixed it its same it does not clone the Frame