What I’m trying to do
When the player clicks an object, allow then to pick that object up and put it down on anything named “Batter_Station_Surface”. For my example I’ve given the player a carrying tray that can be equipped using “E” then I’ve allowed the player to pick up objects named “BakingTray” or “BatterTray” if the player has a bakingtray it can be placed inside on the batter_station_surface and a button can be pressed turning it into a battertray.
What I’ve done to achieve this
I’ve created a local script in player that finds what the player has clicked, checks if it’s “whitelisted” in the clickable objects and then fires a remote event passing through the name of that item.
The script listening for that remote event then has multiple if statements, doing different things depending on what the objects name was.
This achieves what I want to do but I’m not confident that I’ve taken the best direction and wanted some feedback that could perhaps validate my code or suggest a better alternative.
The Code
LocalScript inside starterplayer scripts that monitors clickling and fires server event.
local Players = game:GetService("Players")
Player = Players.LocalPlayer
Mouse = Player:GetMouse()
local Range = 10 -- Range
local Whitelist = {
"BakingTray", "BatterTray",
"give_LargeCircle", "give_LargeSquare",
"Trash", "Sink",
"Batter_Station_Surface", "Batter_Station_Button"
}
Mouse.Button1Down:Connect(function()
local distanceBetweenParts = (Player.Character:FindFirstChild("HumanoidRootPart").Position - Mouse.Target.Position).Magnitude
if distanceBetweenParts < Range then
if table.find(Whitelist, Mouse.Target.Name) then
if Mouse.Target.Parent.Parent:FindFirstChild("Owner").Value == Player.Name then
game.ReplicatedStorage["clickServer"]["onClick"]:FireServer(Mouse.Target.Name, Mouse.Target.Parent.Name)
else
print("You do not have permission to use this.")
end
end
end
end)
Script inside serverscriptservice that listens for events and then peforms an action based on what was clicked.
game.ReplicatedStorage["clickServer"]["onClick"].OnServerEvent:Connect(function(player, Target, Location)
local Bakerys = game.Workspace:FindFirstChild("Bakerys")
local playerBakery = Bakerys:FindFirstChild("Player_Bakery")
if playerBakery ~= false then
if Target == "give_LargeCircle" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == false then
isCarrying.Value = true
local giveTray = game.ReplicatedStorage:FindFirstChild("Baking_Trays"):FindFirstChild("L_Circle"):Clone()
local w = Instance.new("Weld")
w.Parent = giveTray
w.Part0 = w.Parent
w.Part1 = player.Character:FindFirstChild("Tray")
w.C1 = CFrame.new(0, 0.5, 0)*CFrame.Angles(0,0,1.57)
giveTray.Name = "BakingTray"
giveTray.Parent = playerTray
print("Giving Large Circle")
else
print("You are already carrying something!")
end
end
end
if Target == "give_LargeSquare" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == false then
isCarrying.Value = true
local giveTray = game.ReplicatedStorage:FindFirstChild("Baking_Trays"):FindFirstChild("L_Square"):Clone()
local w = Instance.new("Weld")
w.Parent = giveTray
w.Part0 = w.Parent
w.Part1 = player.Character:FindFirstChild("Tray")
w.C1 = CFrame.new(0, 0.5, 0)
giveTray.Name = "BakingTray"
giveTray.Parent = playerTray
print("Giving Large Square")
else
print("You are already carrying something!")
end
end
end
if Target == "Batter_Station_Surface" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == true then
if playerTray:FindFirstChild("BakingTray") then
local BatterStation = playerBakery:FindFirstChild("Batter_Station")
if not BatterStation:FindFirstChild("BakingTray") and not BatterStation:FindFirstChild("BatterTray") then
isCarrying.Value = false
local BakingTray = playerTray:FindFirstChild("BakingTray")
BakingTray:FindFirstChild("Weld"):Destroy()
local w = Instance.new("Weld")
w.Parent = BakingTray
w.Part0 = w.Parent
w.Part1 = BatterStation:FindFirstChild("Batter_Station_Surface")
if BakingTray:FindFirstChild("Shape").Value == "Circle" then
w.C1 = CFrame.new(0, 0.6, 0)*CFrame.Angles(0,0,1.57)
else
w.C1 = CFrame.new(0, 0.6, 0)
end
BakingTray.Parent = BatterStation
end
else
print("This can't be placed here!")
end
end
end
end
if Target == "BakingTray" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == false then
local locateTray = playerBakery:FindFirstChild(Location)
local theTray = locateTray:FindFirstChild("BakingTray")
if theTray then
isCarrying.Value = true
theTray:FindFirstChild("Weld"):Destroy()
local w = Instance.new("Weld")
w.Parent = theTray
w.Part0 = w.Parent
w.Part1 = playerTray
if theTray:FindFirstChild("Shape").Value == "Circle" then
w.C1 = CFrame.new(0, 0.6, 0)*CFrame.Angles(0,0,1.57)
else
w.C1 = CFrame.new(0, 0.6, 0)
end
theTray.Parent = playerTray
else
print("You aren't carrying anything that can be trashed!")
end
end
end
end
if Target == "BatterTray" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == false then
local locateTray = playerBakery:FindFirstChild(Location)
local theTray = locateTray:FindFirstChild("BatterTray")
if theTray then
isCarrying.Value = true
theTray:FindFirstChild("Weld"):Destroy()
local w = Instance.new("Weld")
w.Parent = theTray
w.Part0 = w.Parent
w.Part1 = playerTray
if theTray:FindFirstChild("Shape").Value == "Circle" then
w.C1 = CFrame.new(0, 0.6, 0)*CFrame.Angles(0,0,1.57)
else
w.C1 = CFrame.new(0, 0.6, 0)
end
theTray.Parent = playerTray
else
print("You aren't carrying anything that can be trashed!")
end
end
end
end
if Target == "Batter_Station_Button" then
local Station = playerBakery:FindFirstChild(Location)
local BakingTray = Station:FindFirstChild("BakingTray")
if BakingTray then
BakingTray.BrickColor = BrickColor.new("Salmon")
BakingTray.Name = "BatterTray"
end
end
if Target == "Trash" then
local playerTray = player.Character:FindFirstChild("Tray")
if playerTray then
local isCarrying = playerTray:FindFirstChild("isCarrying")
if isCarrying.Value == true then
isCarrying.Value = false
for i, child in ipairs(playerTray:GetChildren()) do
if child.ClassName == "Part" or child.ClassName == "UnionOperation" then
child:Destroy()
end
end
else
print("You aren't carrying anything that can be trashed!")
end
end
end
end
end)
I hope what I’ve said makes sense, thank you.
If you’d like to have a look for yourself, this place can be copied
Just make sure to change the “Owner” value inside playerBakery to your own username.