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

Tried to make an item giver that gives on touch and didn't work. Any help?

Asked by 7 years ago

So i tried making a giver that gives tool to a player when touched. I used a Local Script cause I need the LocalPlayer.

01local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer")
02local giver = script.Parent
03gave = false
04 
05giver.Touched:connect(function(hit)
06    if hit:FindFirstChild("Humanoid") then
07        if not gave then
08            local playerHammer = hammer:Clone()
09            playerHammer.Parent = game.Players.LocalPlayer.Backpack
10            gave = true
11        end
12    end
13end)

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Hi Derpy,

I don't think .Touched event works in LocalScripts quite as well as ServerScripts. You don't need local player. You can just put the script in a ServerScript but, you need to make it in the Server. Also, LocalScripts don't work in the server, so I have no idea why you're using a LocalScript in the server. I mean, you must be using a LocalScript in the server because the giver is the parent of the LocalScript, and the giver must be in the Server, in this case the workspace, to be seen. So, just replace the LocalScript with a ServerScript. Also, you need to fix up your code a bit because it's wrong. Here, I'll give you an analysis of it down below.

Analysis:

01local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer") -- Ok.
02local giver = script.Parent
03gave = false -- You do not need a boolean.
04 
05giver.Touched:connect(function(hit) -- Use ':Connect()' because, ':connect' is deprecated.
06    if hit:FindFirstChild("Humanoid") then -- Change that to 'if hit.Parent:FindFirstChild("Humanoid") because the hit will be the part under the character that's hit, not the character itself.
07        if not gave then -- Again, you don't need a boolean. Instead, just check if the tool exists in the backpack.
08            local playerHammer = hammer:Clone()
09            playerHammer.Parent = game.Players.LocalPlayer.Backpack -- Again, you don't need local scripts.
10            gave = true -- Don't need a boolean.
11        end -- if statement for this end is not necessary, so you will need to delete this too with the if statement.
12    end
13end)

Here, I'll show you the changes you will have made afterwards:

01local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer");
02local players = game:GetService("Players");
03local giver = script.Parent;
04 
05giver.Touched:Connect(function(hit)
06    if hit.Parent:FindFirstChild("Humanoid") then
07            local player = players:GetPlayerFromCharacter(hit.Parent);
08        local backpack = player:WaitForChild("Backpack");
09 
10        if not backpack:FindFirstChild(hammer.Name) then
11            local playerHammer = hammer:Clone()
12 
13                playerHammer.Parent = backpack;
14        end
15 
16    end
17end)

Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
oh I see, thanks for helping man :D SirDerpyHerp 262 — 7y
0
Np KingLoneCat 2642 — 7y
Ad

Answer this question