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)
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!
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.
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)
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
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)
local r = game.ReplicatedStorage.CoinGive
r.OnServerEvent:Connect(function(player)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10
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:
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)