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

How do I fix this to only give one tool from replicated storage to player's backpack?

Asked by 6 years ago

So I have a map that randomly spawns in and when players get teleported to the map multiple parts randomly appear as they are cloned from the replicated storage (this works properly). This local script constantly check for magnitude between the player and one of those parts and if a player is in range with the part and clicks on a part, the part will disappear and the tool clone from replicated storage will be parented to player's backpack.

This works fine but only once. When I click another part with the same name it does remove it but I don't get the tool in my backpack

Here's a script:


local plr = game.Players.LocalPlayer local mouse = plr:GetMouse() local range = 10 local remote1 = game:GetService("ReplicatedStorage"):WaitForChild("Remote1") local drinkt = game:GetService("ReplicatedStorage"):WaitForChild("drinkTool"):Clone() local foodt = game:GetService("ReplicatedStorage"):WaitForChild("foodTool"):Clone() while wait() do local canPickup = false if mouse.Target then if mouse.Target.Name == "Drink" then print("drink") local drink = mouse.Target if (plr.Character.UpperTorso.Position - drink.Position).magnitude < range then print("in range with drink") canPickup = true if canPickup == true then mouse.Button1Down:Connect(function() canPickup = false print("item clicked") remote1:FireServer(drink) drink:Remove() print("item removed from the client") drinkt.Parent = plr.Backpack end) end end elseif mouse.Target.Name == "Food" then print("food") local food = mouse.Target if (plr.Character.UpperTorso.Position - food.Position).magnitude < range then print("in range with food") canPickup = true if canPickup == true then mouse.Button1Down:Connect(function() canPickup = false print("item clicked") remote1:FireServer(food) food:Remove() print("item removed from the client") foodt.Parent = plr.Backpack end) end end else canPickup = false end else canPickup = false end end

I think it's because the tool variable is cloned only once but again if I put those variables in this while true do loop then when I click on a part I get like 10 of those tools.

Any help is appreciated!

1 answer

Log in to vote
0
Answered by
Astralyst 389 Moderation Voter
6 years ago
Edited 6 years ago

You might want to clone it instead of moving it.

Line 32 and Line 53, once those were ran: "drinkt" and "foodt" will no longer exist since you already moved those into your backpack;

hence will make the script wonder "Where the hell is drinkt (or) foodt" ?

It no longer exists since you already moved them.

Solution:

Line 32

drinkt:Clone().Parent = plr.Backpack

Line 53

foodt:Clone().Parent = plr.Backpack

if you want it so that they can only have one drinktool and foodtool, make a check.

if plr.Backpack and plr.Backpack:FindFirstChild("foodTool") == nil then
foodt:Clone().Parent = plr.Backpack

if you want it so that once they grabbed "foodTool", they can't grab anything else (no duplicates and no second item) then you can just do

if plr.Backpack and plr.Backpack:FindFirstChild("foodTool") == nil and plr.Backpack:FindFirstChild("drinkTool") == nil then
foodt:Clone().Parent = plr.Backpack
0
I tried this but when I click on the part I get like 10 of those tools in my backpack. And when I tried to check if a player already has an item in it's backpack then whenever I click I get 2 items g1o2r3d4a5n6 350 — 6y
0
try adding a cooldown. script.Disabled = true wait(1) script.Disabled = false Astralyst 389 — 6y
Ad

Answer this question