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

My Delete/Replace Item Script Doesn't Work?

Asked by 3 years ago
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character

character.ChildAdded:Connect(function(tool)
    if tool:IsA("Tool") and tool.Name:is("sharpness") then
        player.Backpack.ClassicSword:Destroy()
        game.ServerStorage.ClassicSword2:Clone().Parent = player.Backpack
    end
end)

I don't fully understand why this script isn't working- I'm trying to make it so that when "sharpness" is equipped, "ClassicSword" is deleted from the player's backpack and "ClassicSword2" is cloned into their backpack. Why isn't my script working?

1 answer

Log in to vote
0
Answered by 3 years ago

ServerStorage can only be accessed by the server. You're going to have to fire a remote event and have a server script parent the sword for you.

Make a RemoteEvent and place it in ReplicatedStorage.

local remoteEvent = game.ReplicatedStorage.RemoteEvent

character.ChildAdded:Connect(function(tool)
    remoteEvent:FireServer(tool) --it's more secure to run the check through the server, because hackers or whatever
end)

In a server Script placed in ServerScriptStorage:

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player,tool) -- first parameter is always player, second parameter is what you sent through the remote event from the local script which in this case is the child that has been added to the character
    if tool:IsA("Tool") and tool.Name:is("sharpness") then
        player.Backpack.ClassicSword:Destroy()
        game.ServerStorage.ClassicSword2:Clone().Parent = player.Backpack
    end
end)

Let me know if you run into any issues.

Ad

Answer this question