Provided this is a tool, you can use a ServerScript just fine by changing up some of the reference variables to your player and respective character. Remember when a player spawns and has tools put into their backpack you can find the player pretty easily by going up the hierarchy tree.
The backpack will be the tool’s parent. So long as it is not INSTANTLY equipped by the player using :EquipTool()
The player will be the backpack’s parent.
Changing up some of these variables will fix this script for use on the server.
This here is where you need to alter. Think about the ancestry of your script and how you can traverse through the ancestry tree to get up to a higher level; where your player will be.
One method to make this server sided is by using RemoteEvents. When you equip the tool, signal a RemoteEvent sending only the Player from the local script, and doing stuff you need to change in the script receiving the signal.
This is one of many methods you can use. I’ve been doing this most of the time when it comes to tools and other jazz.
Well, you can fetch the character from the .Equipped event (the tools parent)
One of the most secure ways to make tools that don’t require the use of the client’s Mouse.
local Char --gonna become the character later on
local Humanoid
Tool.Equipped:Connect(function()
Char = Tool.Parent --since the tool is a child of the char when equipped
Humanoid = Char:WaitForChild("Humanoid") --now you have the characters humanoid
end)
Tool.Unequipped:Connect(function()
--still have reference to the char and humanoid since they're global variables
end)
You can convert the character to a player/user using game.Players:GetPlayerFromCharacter(Character: Model).
And you can also check if Char and Humanoid are nil, if they aren’t you don’t have to set it once again.
Slight grammar mistakes.
AND if you are doing PlayerAdded with something like this:
local Player
game.Players.PlayerAdded:Connect(function(User)
Player = User
end)
Please bear in mind the player variable is able to be changed to anyone that joins the game, and will not be the local player that joined.
A remoteEvent is communication between the server and client. So you could fire a remoteEvent from the client to the server (server executes code when client fires the event). That way you can listen for the equipped event and still make some code on the server
to fire a client from the client when a player equips the tool you can do:
Client:
local player = game.Players.LocalPlayer
local tool = script.Parent
local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
tool.Equipped:Connect(function()
remoteEvent:FireServer()--fire the event
end)
For server, you want to listen to the event and receive it when it gets fired:
local remoteEvent = Instance.new("RemoteEvent", game.ReplicatedStorage)
remoteEvent.OnServerEvent:Connect(function(player) --player is the argument passed through the event by the client
--do your code here
local char = player.Character
--local clone = ...:Clone()
--clone.Parent = char
--etc.
end)
Just like how @HitCraftPizzeria demonstrated. This is one of the most reliable way of firing from client to server. But do keep in mind the client/server “lag.” This can make your player’s character choppy, especially when your are attaching something to the physical character itself (from the server side).