Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Replicating issue. When walking on a block a sword is given, but the sword does not function?

So I am creating this new swordfighting game, and basically I am making it so when you walk on a pad where you swordfight, you're given a linked sword.

And also, when you go to the grass to get off the pad, the sword is removed from your inventory which is what is expected and is normal.

However, when I try and swing the sword when I am on the pad, there is nothing happening. No lunging or sounds.

Here is the localscript code for when a player walks on the swordfighting pad to swordfight

local part = game.Workspace.Pad
local player = game.Players.LocalPlayer
local sword = game.ReplicatedStorage.ClassicSword

part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		game.ReplicatedStorage["ClassicSword"]:Clone().Parent = player.Backpack
	end
end)

This code here is the main issue.

Any help is really appreciated.

P.S. I'm new to scripting.

2 comments

0

I am also going to add a debounce now.

0

Because the sword does not exist on the server, only on the client as you cloned in a local script. Just move the code into a server script. "player" would be game.Players:GetPlayerFromCharacter(hit.Parent).

1 answer

For first, you can not use game.Players.LocalPlayer in ServerScript only in LocalScript. and you can get player with .Touched

Here is a example:

-- Get player with touch event

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- Check if is player character touched
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player
		local char = hit.Parent -- Player Character.
		if player then -- Check if is player.
			print("I found player: " .. player.Name)
		end
	end
end)

Here is your fixed script:

-- Server Script(Script)
local part = workspace:WaitForChild("Pad") -- Changed game.Workspace to workspace Also added a WaitForChild
-- Removed this player variable but you can not use game.Players.LocalPlayer in ServerScript.
local sword = game:GetService("ReplicatedStorage"):WaitForChild("ClassicSword") -- Added GetService and WaitForChild

part.Touched:Connect(function(hit)
	-- Removed human variable
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player touched
		if player then -- Check if player exists
			-- Changed your code here, but you already have variable for sword
			sword:Clone().Parent = player:WaitForChild("Backpack") -- Added a WaitForChild for Backpack.
		end	
	end
end)

Hope it helped :D

Wiki pages:

GetPlayerFromCharacter

WaitForChild

GetService

Tutorials for create a game on roblox / Start scripting:

Roblox Wiki - Get Start

ScriptingHelpers - Guides

Errors? tell-me on comments.
Solved your problems? put in title [SOLVED] or accept a answer.

5 comments

0

Sword still refuses to work.

0

So the problem is in the script of your sword, try with another tool

0

I have been looking all over for a sword that actually works, but to no avail. Could you possibly link me one because this endless search comes up with no results.

0

Try to disable all scripts, and on clone active this scripts.

0

Touched works in local scripts, just not in this manner

This discussion is preserved as it was. New answers, comments, and votes are closed.