Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Tried to make a item giver using a click detector, but doesn't work??

I am currently working on my game, Roblox HD, I decided to make a Bloxy Cola giver (and I had make a cooling box for drinks as well for the Bloxy Colas), I made them, then I scripted them, but when I tested the giver, it doen't give me a single Bloxy Cola at all.

Code: (LocalScript, relies on a part named InteractablePart and a ClickDetector inside the part, also relies a folder named SharedObjects, the Bloxy Cola tool is inside that folder)

repeat wait() until game.ReplicatedStorage:WaitForChild("SharedObjects")

local plr = game.Players.LocalPlayer
local ColaTool = game.ReplicatedStorage.SharedObjects.BloxyCola
local ClickDetector = script.Parent.InteractablePart.ClickDetector

ClickDetector.MouseClick:Connect(function()
	local cloned = ColaTool:Clone()
	cloned.Parent = plr.Backpack
end)

At first, I thought that the repeat wait that I use was the cause (I use the repeat wait so that it will wait until the SharedObjects folder to load, the Bloxy Cola is inside the folder), but when I removed the line, it still doesn't work.

No errors show up in the console.

Game link: https://web.roblox.com/games/4715818343/Roblox-HD

2 comments

0

repeat wait() until game.ReplicatedStorage:WaitForChild("SharedObjects") this is kinda residual.

0

better add a wait at the top rather than repeat, until

2 answers

✓ Accepted answer

Use a normal script instead of a local one because functions performed by local scripts aren't sent to the server, so if you make yourself invisible, for example you'll be invisible only to yourself!

Also click-detectors have a built-in player argument so you don't have to find the player. It should be as simple as this:

script.Parent.MouseClick:Connect(function(player)
	local colaTool = script.Parent.Cola
	local colaClone = colaTool:Clone()
	colaClone.Parent = player.Backpack
end)

Hope this helps.

No, no nononononononooooooo

never use a LocalScript when you're doing something inside the Server. LocalScripts are meant to do only client-sided stuff

put this code in a normal script and it should work:

repeat wait() until game.ReplicatedStorage:WaitForChild("SharedObjects")

local ColaTool = game.ReplicatedStorage.SharedObjects.BloxyCola
local ClickDetector = script.Parent.InteractablePart.ClickDetector

ClickDetector.MouseClick:Connect(function(plr) -- MouseClick actually passes the player who clicked the object as an argument, so you don't need to define a variable for the player BEFORE he clickes
    local cloned = ColaTool:Clone()
    cloned.Parent = plr.Backpack
end)

1 comment

0

This is the same awnser as above as well

This discussion is preserved as it was. New answers, comments, and votes are closed.