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

?[Help] Touch part to give a specific tool

Asked by
c0nc3rt 61
4 years ago

My problem

I want to make a part, if i touch it, it should give me a tool(i putted it in ReplicatedFirst). I tried to make it in FilteringEnabled, but nope. I also tried it in FilteringDisabled but not work too. Here's the code. FilteringEnabled

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword
local backpack = Players.LocalPlayer.Backpack

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    sword:Clone()
    sword.Parent = backpack
end
part.Touched:Connect(onTouched)

****FilteringDisabled

local Players = game.Players
local sword = game.ReplicatedFirst.ClassicSword
local backpack = Players.LocalPlayer.Backpack

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    sword:Clone()
    sword.Parent = backpack
end
part.Touched:Connect(onTouched)

Hope you guys help me. Im new in scripting in Roblox. :)

1 answer

Log in to vote
0
Answered by 4 years ago

You're trying to reference LocalPlayer from a server script (or at least I'd think you'd be using a server script for this), which doesn't work as it runs on the server. Try this:

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    local backpack = player.Backpack
    sword:Clone().Parent = backpack
end
part.Touched:Connect(onTouched)

Also, you should probably use ReplicatedStorage or ServerStorage instead, since the sword does not need to be replicated to the client first. If you used ServerStorage, you would simply just change line 2 to local sword = game.ServerStorage.ClassicSword, and of course move the sword to ServerStorage.

0
Thanks c0nc3rt 61 — 4y
Ad

Answer this question