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

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

Asked by 4 years ago

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

0
repeat wait() until game.ReplicatedStorage:WaitForChild("SharedObjects") this is kinda residual. Block_manvn 395 — 4y
0
better add a wait at the top rather than repeat, until hallmk7 50 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago

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.

Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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)
0
This is the same awnser as above as well RadiatedExodus 41 — 4y

Answer this question