for some reason when I use a tool from the shop tools it doesn’t work. But if I directly place it into starter pack the tool works fine. So is there anything wrong with these scripts?
--Server Script, tool cloning and giving
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("ToolDataStore")
local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("PurchaseTool")
local toolsFolder = game.ReplicatedFirst:WaitForChild("ShopTools")
local doSaving = true
local Players = game:GetService("Players")
local GuiService = game:GetService("GuiService")
local function save(plr)
if doSaving then
local tools = {}
for parent, tool in pairs(plr.Inventory:GetChildren()) do
if tool:IsA("Tool") then
table.insert(tools, tool.Name)
end
end
local success, errorMsg = pcall(function()
DS:SetAsync(plr.UserId .. "-key", tools)
end)
end
end
local function load(plr)
if doSaving then
local data = nil
local success, errorMsg = pcall(function()
data = DS:GetAsync(plr.UserId .. "-key")
end)
if data ~= nil then
for tools, tool in pairs(data) do
local foundTool = toolsFolder:FindFirstChild(tool)
if foundTool then
foundTool:Clone().Parent = plr:WaitForChild("Inventory")
end
end
end
end
return
end
Event.OnServerEvent:Connect(function(plr, toolName)
local foundTool = toolsFolder:FindFirstChild(toolName)
if foundTool then
local price = 0
if foundTool:FindFirstChild("Price") then
price = foundTool.Price.Value
end
if plr.leaderstats.Coins.Value >= price then
plr.leaderstats.Coins.Value -= price
local newtool = foundTool:Clone()
newtool.Parent = plr:WaitForChild("Inventory")
end
end
end)
Players.PlayerAdded:Connect(load)
Players.PlayerRemoving:Connect(save)
--server script, Hitbox script
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local db = false
local plrs = game:GetService("Players")
local plr = plrs:GetPlayers()[1] or plrs.PlayerAdded:Wait()
local touched = false
local slash = script.Parent.Slash
script.Parent.HitBoxEvent.OnServerEvent:Connect(function()
if db then return end
db = true
local Hitbox = Instance.new("Part")
Hitbox.Parent = workspace
Hitbox.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3)
Hitbox.Transparency = .4
Hitbox.Anchored = true
Hitbox.Size = Vector3.new(7, 7, 5)
Hitbox.CanCollide = false
local HitChar = {}
-- we use here task.spawn to run the check in a separate thread
task.spawn(function()
for i = 1, 10 do -- Check 10 times (you can adjust this number depends on what you want)
local parts = workspace:GetPartsInPart(Hitbox)
for _, part in ipairs(parts) do
local character = part:FindFirstAncestorOfClass("Model")
if character and character:FindFirstChild("NPC") and not table.find(HitChar, character) and not touched then
touched = true
table.insert(HitChar, character)
character.Humanoid:TakeDamage(5)
if character.Humanoid.Health == 0 then
plr.leaderstats.Coins.Value += 10
end
end
end
task.wait(0.1) -- Wait 0.1 seconds between each check
end
Hitbox:Destroy() -- Destroy the hitbox after we're done
end)
task.wait(1)
touched = false
db = false
end)
-- local script, transfer script
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Katana" and plr.Inventory:FindFirstChild("Katana") then
script.Parent.Click:Play()
plr.Inventory.Katana.Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Better Katana" and plr.Inventory:FindFirstChild("Better Katana") then
script.Parent.Click:Play()
plr.Inventory:FindFirstChild("Better Katana").Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Blade of light" and plr.Inventory:FindFirstChild("Blade of light") then
script.Parent.Click:Play()
plr.Inventory:FindFirstChild("Blade of light").Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Merciless blade" and plr.Inventory:FindFirstChild("Merciless blade") then
script.Parent.Click:Play()
plr.Inventory:FindFirstChild("Merciless blade").Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Warriors katana" and plr.Inventory:FindFirstChild("Warriors katana") then
script.Parent.Click:Play()
plr.Inventory:FindFirstChild("Warriors katana").Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.ToolName.Text == "Wood Sword" and plr.Inventory:FindFirstChild("Wood Sword") then
script.Parent.Click:Play()
plr.Inventory:FindFirstChild("Wood Sword").Parent = plr.Backpack
script.Parent.Visible = false
script.Parent.Parent.Unequip.Visible = true
end
end)
All help is appreciated! Also, the reason I am showing the shop scripts as well is because I think the problem may lie within the cloning process. Also showing the inventory script bc it might be bc the transfer process. I know its a lot to review, sorry about that.