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 6 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.

local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer")
local giver = script.Parent
gave = false

giver.Touched:connect(function(hit)
    if hit:FindFirstChild("Humanoid") then
        if not gave then
            local playerHammer = hammer:Clone()
            playerHammer.Parent = game.Players.LocalPlayer.Backpack
            gave = true
        end 
    end
end)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 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:

local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer") -- Ok.
local giver = script.Parent 
gave = false -- You do not need a boolean.

giver.Touched:connect(function(hit) -- Use ':Connect()' because, ':connect' is deprecated.
    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.
        if not gave then -- Again, you don't need a boolean. Instead, just check if the tool exists in the backpack.
            local playerHammer = hammer:Clone() 
            playerHammer.Parent = game.Players.LocalPlayer.Backpack -- Again, you don't need local scripts.
            gave = true -- Don't need a boolean.
        end -- if statement for this end is not necessary, so you will need to delete this too with the if statement.
    end
end)

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

local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer");
local players = game:GetService("Players");
local giver = script.Parent;

giver.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
            local player = players:GetPlayerFromCharacter(hit.Parent);
        local backpack = player:WaitForChild("Backpack");

        if not backpack:FindFirstChild(hammer.Name) then
            local playerHammer = hammer:Clone()

                playerHammer.Parent = backpack;
        end

    end
end)

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 — 6y
0
Np KingLoneCat 2642 — 6y
Ad

Answer this question