I made a shop GUI, and players are able to buy swords from it.
When someone first buys a sword, the damage script inside of the sword is being completely ignored for some reason.
I changed the it to a local script and it started to work fine.
I was wondering why was the damage script being ignored? If the player resets then the damage script works just fine, but when first buying the sword it isn't being run at all.
ShopGUI script:
local katanaButton = script.Parent local player = game.Players.LocalPlayer local replicatedStorage = game:GetService("ReplicatedStorage") local storedKatana = game.replicatedStorage.Katana katanaButton.MouseButton1Click:connect(function() if player.Backpack.Money.Value >= 100 and not player.Character:FindFirstChild("Katana") then local Katana = storedKatana:Clone() Katana.Parent = player.Character local RootPart = player.Character:FindFirstChild("HumanoidRootPart") Katana.CFrame = player.Character:FindFirstChild("Left Leg").CFrame * CFrame.new(-.6, .2, 1.7) Katana.CFrame = Katana.CFrame * CFrame.Angles(.3, 0, 1.5) local weld = Instance.new("Weld") weld.Part0 = RootPart weld.C0 = RootPart.CFrame:inverse() weld.Part1 = Katana weld.C1 = Katana.CFrame:inverse() weld.Parent = Katana player.Backpack.Money.Value = player.Backpack.Money.Value - 100 script.Parent.Parent.TextLabel.Text = "Thank you, for your purchase!" wait(2) script.Parent.Parent.TextLabel.Text = "Sword Shop" local sword = Instance.new("StringValue") sword.Parent = player.StarterGear sword.Name = "Sword" sword.Value = "Katana" else if player.Character:FindFirstChild("Katana") then script.Parent.Parent.TextLabel.Text = "You already bought a Katana" wait(2) script.Parent.Parent.TextLabel.Text = "Sword Shop" elseif player.Backpack.Money.Value <= 100 then local cashreq = 100 - player.Backpack.Money.Value script.Parent.Parent.TextLabel.Text = "You need "..cashreq.." more money to purchase this" wait(2) script.Parent.Parent.TextLabel.Text = "Sword Shop" else script.Parent.Parent.TextLabel.Text = "Something went wrong with the script" wait(2) script.Parent.Parent.TextLabel.Text = "Sword Shop" end end end)
Respawn script:
replicatedStorage = game:GetService("ReplicatedStorage") storedKatana = game.replicatedStorage.Katana game.Players.PlayerAdded:connect(function(player) local sword = player:WaitForChild("StarterGear"):WaitForChild("Sword") if sword then if sword.Value == "Katana" then player.CharacterAdded:connect(function(character) local Katana = storedKatana:Clone() Katana.Parent = player.Character local RootPart = player.Character:FindFirstChild("HumanoidRootPart") Katana.CFrame = player.Character:FindFirstChild("Left Leg").CFrame * CFrame.new(-.6, .2, 1.7) Katana.CFrame = Katana.CFrame * CFrame.Angles(.3, 0, 1.5) local weld = Instance.new("Weld") weld.Part0 = RootPart weld.C0 = RootPart.CFrame:inverse() weld.Part1 = Katana weld.C1 = Katana.CFrame:inverse() weld.Parent = Katana end) else print("something went wrong! 2") end else print("Something went wrong! 1") end end)
Damage Script:
katana = script.Parent player = game.Players.LocalPlayer swingDamage = script.Parent.SwordDamage.Value CanDamage = true katana.Touched:connect(function(hit) local humanoid = hit.Parent:WaitForChild("Humanoid") if humanoid then if humanoid.Parent.Name ~= script.Parent.Parent.Name and CanDamage and script.Parent.Swinging.Value == true then CanDamage = false humanoid:TakeDamage(swingDamage + script.Parent.Combo.Value) print("Dealt ", swingDamage + script.Parent.Combo.Value, " damage") wait(1) CanDamage = true else print("something went wrong2") end else print("something went wrong1") end print("something went wrong0") end)