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)
1 | local RemovePortal = script.Parent |
2 | local player = game.Players.LocalPlayer |
3 | RemovePortal.Touched:Connect( function (hit) |
4 | local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part |
5 | if hit = = DirtyRock then |
6 | DirtyRock:Destroy() |
7 | player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 |
8 | end |
9 | 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:
01 | local RemovePortal = script.Parent |
02 | RemovePortal.Touched:Connect( function (hit) |
03 | local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part |
04 | if hit = = DirtyRock then |
05 | DirtyRock:Destroy() |
06 | for _,player in pairs (game.Players:GetPlayers()) do |
07 | player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 |
08 | end |
09 | end |
10 | end ) |
It appears you are trying to use game.Players.LocalPlayer in a server script try this script:
01 | local RemovePortal = script.Parent |
02 | RemovePortal.Touched:Connect( function (hit) |
03 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
04 | if not player then return end |
05 | local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part |
06 | if hit = = DirtyRock then |
07 | DirtyRock:Destroy() |
08 | player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 |
09 | end |
10 | end ) |
create a script inside the ServerScriptService and add this;
1 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
2 | local Bin = Instance.new( "RemoteFunction" , ReplicatedStorage) |
3 | Bin.Name = "Add" |
4 |
5 | local function add(player) |
6 | player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5 |
7 | end |
8 | Add.OnServerInvoke = add |
inside a LocalScript in the UI
01 | local Add = game:GetService( "ReplicatedStorage" ):WaitForChild( "Add" ) |
02 | local RemovePortal = script.Parent |
03 | local player = game.Players.LocalPlayer |
04 | RemovePortal.Touched:Connect( function (hit) |
05 | local DirtyRock = game.Workspace.Tycoon.DropPortal.Rocks.Part |
06 | if hit = = DirtyRock then |
07 | DirtyRock:Destroy() |
08 | Add:InvokeServer() |
09 | end |
10 | end ) |