Collectible Coin & Kill Player after

What do you want to achieve? I want the coin to kill the player after it collects and gives the player 10 coins towards the leaderboard.

What is the issue? I can’t figure out how to kill the player.

What solutions have you tried so far? I have tried looking up tutorials and looked at the dev forum. I am not good with coding but I have got this far…

Here is the coin script that I have so far…

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
			script.Sound:Play()
			wait(60)
			db = true
			script.Parent.Transparency = 0
			print('RespawnedHardCoin!')
		end
	end	
end)

forrobloxscripthelp3
I have also put a remote event in Replicated Storage.
I have no idea how remote event works and every time I try it always fails, so if you are going to talk about remote events please give example.
Thank you for any help!

1 Like

From the Holy Texts of Game Security

If a player’s health or score is controlled by the server, a hacker cannot change these values in a way that will affect other players.

With that out of the way I do recommend using a Remote Event that fires when the player touches the coin.
That Remote Event can simply change the Player’s health by finding the Player Character in the workspace, and changing the Humanoid Health to 0
game.Workspace:FindFirstChild(player).Humanoid.Health = 0
of course you should put a sanity check to make sure the player hasn’t somehow disappeared in the miliseconds it takes to send the remoteEvent.

Sorry, I don’t understand Remote Events all to well. How would you go about doing that?

In your code instead of changing the leaderstats once you have confirmed it is a valid player you would fire a remote event to the server. On the server side of things you would wait for that remote and once called you would award the player who fired it which is always the first argument in remotes, but you will need safe guards to prevent this remote from being exploited.
Client Side:

local r = workspace.RemoteName
r:FireServer()

Server Side:

local r = workspace.RemoteName
r.OnServerEvent:Connect(function(player)
 player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
end)
1 Like

Don’t do exactly what @cheetamcu just wrote out. RemoteEvents should be stored in Replicated Storage.

However, his model is clean enough, just make sure to put the remoteevents into ReplicatedStorage and get them from there when you want to fire them.

No. Where would you possibly place this on the server? The remote event has to be fired from the Client, (local) to the server(global). the touched Event will fire locally for the client, so you have to get the Touched event, and then fire to the server to edit the player health and the coin Value.

EDIT
Your second one there the

local r = game.ReplicatedStorage.CoinGive
r:FireServer()

is alright, but you need to attach that to the Touched event inside a script inside the part. which then connects to OnServerEvent in the server script

So this part needs to be on client?

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
			script.Sound:Play()
			wait(60)
			db = true
			script.Parent.Transparency = 0
			print('RespawnedHardCoin!')
		end
	end	
end)

Then this needs to be on server???

local r = game.ReplicatedStorage.CoinGive
r.OnServerEvent:Connect(function(player)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10

I tried this but it gave everyone 10 coins including the player who touched the coin.

Hey, if you had your code inside of a script, it’s okay. Here is a fixed version of your script, so it kills a person after he collects a coin also it would be better if you’d use a remote events for increasing and decreasing values of a player such as leaderstats.

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
			script.Sound:Play()
            workspace:FindFirstChild(hit.Parent.Name):FindFirstChild("Humanoid").Health = 0
			wait(60)
			db = true
			script.Parent.Transparency = 0
			print('RespawnedHardCoin!')
		end
	end	
end)

The only thing I have added inside of script is this piece of code:

workspace:FindFirstChild(hit.Parent.Name):FindFirstChild("Humanoid").Health = 0

BindableEvent

Server script to server script event I am going to show you how to use it. Make sure to store bindable event inside of replicated storage.

Your touch script:

local db = true
local Event = game:GetService("ReplicatedStorage"):WaitForChild("BindableEventName")

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            Event:Fire(hit.Parent,"Coins",10)
			script.Sound:Play()
            workspace:FindFirstChild(hit.Parent.Name):FindFirstChild("Humanoid").Health = 0
			wait(60)
			db = true
			script.Parent.Transparency = 0
			print('RespawnedHardCoin!')
		end
	end	
end)

New script inside serverscriptservice:

local Bindable = game:GetService("ReplicatedStorage"):WaitForChild("BindableEventName")

Bindable.Event:Connect(function(player,type_leaderstats,amount)
    game:GetService("Players"):FindFirstChild(player.Name).leaderstats[type_leaderstats].Value =  game:GetService("Players"):FindFirstChild(player.Name).leaderstats[type_leaderstats].Value + amount
end)
3 Likes

Thanks, I’ll try this. I’ll try to get back as soon as possible.

I tried this and I got forrobloxscripthelp2 did I forget to put something in?

local Bindable = game:GetService("ReplicatedStorage"):WaitForChild("GiveCoins")

Bindable.Event:Connect(function(player,Coins,amount)
    game:GetService("Players"):FindFirstChild(player.Name).leaderstats[Coins].Value =  game:GetService("Players"):FindFirstChild(player.Name).leaderstats[Coins].Value + amount
end)

Is GiveCoins BindableEvent or RemoteEvent?

GiveCoins is a RemoteEvent, oh… it’s supposed to be Bindable isn’t it?

Yeah, it is supposed to be BindableEvent.

Yeah, it’s fixed now! Thanks @BenMactavsin!