Tool Script (LocalScript) -- Tool is in StarterPack Tool.Name is the ore name
tool = script.Parent ReplicatedStorage = game.ReplicatedStorage dropEvent = ReplicatedStorage:WaitForChild("dropEvent") function drop() local oreName = tool.Name local Player = tool.Parent.Name print("Calling Event") dropEvent:FireServer(Player, tool.Name, tool) tool.Number.Value = tool.Number.Value - 1 verif() end function verif() if tool.Number.Value <= 0 then tool:Destroy() end end tool.Activated:Connect(drop)
Event Script (server Script) -- In ServerScriptService
ReplicatedStorage = game.ReplicatedStorage dropEvent = Instance.new("RemoteEvent", ReplicatedStorage) dropEvent.Name = "dropEvent" local function drop(Player, oreName, tool) print("Event called") print(Player, " ", oreName, " ", tool) local Ore = game.ReplicatedStorage.Ores:FindFirstChild(oreName) local newOre = Ore:Clone() newOre.Owner.Value = Player newOre.Name = "Ore" newOre.Ore.Value = oreName newOre.Parent = workspace.Loots end dropEvent.OnServerEvent:Connect(drop)
-- Studio Test Output -- Calling Event Event called alancretel1999 alancretel1999 Stone 13:14:15.214 - ServerScriptService.lootOre:10: attempt to index local 'Ore' (a nil value) 13:14:15.216 - Stack Begin 13:14:15.218 - Script 'ServerScriptService.lootOre', Line 10 13:14:15.219 - Stack End
-- Studio Team Test Output -- Calling Event
Thanks for your help, I'm new inLocalScript and custom events
From what I can conclude, the function:
local Ore = game.ReplicatedStorage.Ores:FindFirstChild(oreName) local newOre = Ore:Clone()
Ore is filled with a "nil" value because the first child named OreName can not be found. This means that either your Ore is not named oreName, or it is not located in ReplicatedStorage.Ores.
Make sure that the script is INSIDE the tool. It is also better to use:
local Player = game.Players.LocalPlayer
instead of:
local Player = tool.Parent.Name
because it might mess up when multiple players are ingame at the same time
When you fired the server event, you fired the player along with it. You don't need to do this, it'll add 2 players as the arguments of the function on the server script.
So instead of doing this:
dropEvent:FireServer(Player, tool.Name, tool)
Drop out the "Player" variable. It'll fix the arguments. Player will always be an added argument to this.