[Closed!]Need help with HIT function on LocalScript

Hi there! I have a little problem, I want to make a coin for my simulator and want to make the coin in a local script so pros are not able too able to steal all of the coins for the new players.
I only don’t know how to do this! I tried the normal HIT function:

Untitledrerfdcxzxz d

But that didn’t work! Can someone help me with this? I don’t know if there is a special function for this, I already did some research but didn’t found anything.

I hope you can help me!
Thanks for reading! :slightly_smiling_face:

i think this might work, i never have used hit in a player:

--[[local Player = game.Players.LocalPlayers
local Char = Player.Character <-- I think you should ignore this  ]]

local Object = game.Workspace.Coin.PrimaryPart
---//Code//---
Object.Touched:Connect(function(hit)
    if hit:FindFirstChild("Humanoid") ~= nil then
        --//Code
    end
end)

if that dont works then try this:

local Player = game.Players.LocalPlayers
local Char = Player.Character

local Object = game.Workspace.Coin.PrimaryPart
---//Code//---
Object.Touched:Connect(function(Char)
    if Char:FindFirstChild("Humanoid") ~= nil then
        --//Code
    end
end)

you don’t have to do ~= nil, if you delete that it should work

Edit: oop, didn’t read properly. Local scripts don’t work in the workspace

1 Like

It looks like you are trying to run this LocalScript in workspace; LocalScript can’t run in workspace because they need to be placed under the client. You should place your LocalScripts in places such as StarterGui or StarterPlayerScripts. If you want to have a script under each coin you will have to use a server script to do this.

It looks like you have a LocalScript under every coin so this means you wont be able to have one LocalScript under the client controlling everything. However if you adjust your code you can get it to work with one LocalScript under the client:

local Coins = workspace.Coins -- Folder full of coins

for i, v in pairs(workspace.Coins:GetChildren()) do
	local Connection
	
	Connection = v.Touched:Connect(function(Hit)
		if Hit:FindFirstChild("Humanoid") then
			-- Do stuff when the coin is touched
			Connection:Disconnect() -- Removes the connection to the coin
			-- You may want to destroy the coin			
		end
	end)
end

2 Likes

I tried it but it doesn’t work.

How doesn’t it work?
Any errors?
Have you tried debugging using either prints or the debugger?

@watterrunner’s script doesn’t do anything, I tried to print something to, but that doesn’t do anything to and I am not getting any errors.

Did you place the LocalScript in a place it’ll run?

Yes, I did that. I placed in StarterGui and later on I placed it in StarterPlayerScripts, both don’t work.

The code that I provided is meant to be ran from a single script placed somewhere on the client rather than multiple scrips under each coin. If you want to have a script under each coin you need to use a server script to do this using the code that you provided.

The code that I provided is only meant to be used as an example because it is missing lots of the core functionality that you are most likely after. I haven’t added in any of the coin adding features, removal of coins or loading new coins because I don’t know how your system is set up. You should edit my code where I have commented to add that functionality in.


If you would like some support on adding the functionality that I explained above please could you provide more information on what you want to achieve and how your system is set up. Please remember the scripting support category isn’t a place for people to make entire systems for you.

Okay, I will try to make it work! Thanks!

You have everything correct, and as efficient as it is in that script. Other scripts people may post will have variables for the coin, but if you put this script you made in the hit-box part, you can keep it. You will just need to add a script inside the IF-THEN statement, as I do not know the variables you need for calling up parts in the Workspace. Adding a player check will require adding values into maybe the player’s toolbox, but you can do whatever you want. I am just here to tell you that your current script should work to find the player who came into contact with the coin, but I can not tell you how to add a filter command, as I do not know your contents in-game. I hope this works as the solution to your original question! :wink:

EDIT: If you are using more than a few coins, you could use @waterrunner’s script, my idea is just because it is easier with what you have, if you only have a few you could duplicate.