Cup giver not working?

local DB = false
local ClickDetector = game.workspace.CupGiver.CupPart.ClickDetector

ClickDetector.MouseClick:Connect(function()
	if DB == false then
		DB = true
		local plr = game:GetService("Players").LocalPlayer
		local cup = game.ServerStorage:WaitForChild("Drinks"):WaitForChild("Cup"):Clone()
		cup.Parent = plr("Backpack")
		wait(3)
		DB = false
	end
end)

Error: 12:45:03.260 - Workspace.CupGiver.CupPart.Script:9: attempt to call a nil value

Workspace: https://gyazo.com/51a7b9be4f5a7707bbb906b450fd1d42

Is this in a local script of a server script?

You can’t use localplayer in a serverscript.

1 Like

I think you need square brackets for the cup.Parent

cup.Parent = plr["Backpack"]
--or
cup.Parent = plr.Backpack

You can use click.Backpack (correct me if i’m wrong)

local DB = false
local ClickDetector = game.workspace.CupGiver.CupPart.ClickDetector

ClickDetector.MouseClick:Connect(function(click)
	if DB == false then
		DB = true
		local plr = game:GetService("Players").LocalPlayer
		local cup = game.ServerStorage:WaitForChild("Drinks"):WaitForChild("Cup"):Clone()
		cup.Parent = click.Backpack
		wait(3)
		DB = false
	end
end)

.MouseClick has a parameter called playerWhoClicked, and the parameter is a Player instance, so instead of trying to use game.Players.LocalPlayer on the server you should instead use that parameter.