Hi! I got a Touched event which I want a part called "DirtyRock" to be hit/h (the parameter of touched event) and when that happens, I want the DirtyRock to be destroyed and add a more value to the leaderstats.Money.Value. Here is my script: (it doesn't work)
local RemovePortal = script.Parent local player = game.Players.LocalPlayer RemovePortal.Touched:Connect(function(hit) local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part if hit == DirtyRock then DirtyRock:Destroy() player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 end end)
RemovePortal is the script that will have the event in it meaning its RemovePortal.Touched (keep in mind the script is in RemovePortal)
Players.LocalPlayer only works in a LocalScript. If you call It in a regular Script, It will return nil.
I suppose you're trying to make a script where each player gets money when the "DirtyRock" gets touched by "RemovePortal". In that case, you should try this:
local RemovePortal = script.Parent RemovePortal.Touched:Connect(function(hit) local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part if hit == DirtyRock then DirtyRock:Destroy() for _,player in pairs (game.Players:GetPlayers()) do player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 end end end)
It appears you are trying to use game.Players.LocalPlayer in a server script try this script:
local RemovePortal = script.Parent RemovePortal.Touched:Connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if not player then return end local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part if hit == DirtyRock then DirtyRock:Destroy() player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 end end)
create a script inside the ServerScriptService and add this;
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Bin = Instance.new("RemoteFunction", ReplicatedStorage) Bin.Name = "Add" local function add(player) player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 end Add.OnServerInvoke = add
inside a LocalScript in the UI
local Add = game:GetService("ReplicatedStorage"):WaitForChild("Add") local RemovePortal = script.Parent local player = game.Players.LocalPlayer RemovePortal.Touched:Connect(function(hit) local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part if hit == DirtyRock then DirtyRock:Destroy() Add:InvokeServer() end end)