Good day,
I’m having difficulties getting this script I have in a model to work after cloning it from ReplicatedStorage to the client’s workspace. In specific, it won’t detect when the player touches the part. The script itself works perfectly fine when it’s normally in the workspace, for whatever reason though, it doesn’t when I clone the model it’s in to the client’s.
Here’s the code (it is in a regular script):
local pumpkinPart = script.Parent.Parent
local amount = 1
local toggle = false
script.Parent.Touched:Connect(function(hit)
print("Start...")
if hit.Parent:FindFirstChild("Humanoid") ~= nil and toggle == false then
toggle = true
print("Pumpkin Touched..")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local leaderboard = player:FindFirstChild("Pumpkin Leaderboard")
local pumpkins = leaderboard.Pumpkins
pumpkins.Value = pumpkins.Value + amount
print("Added to total..")
wait(0.5)
pumpkinPart:Destroy()
end
end)
Where the script is within the model:
I have already tried disabling the script in studio and then enabling it once cloned into the client’s workspace, nothing changed. I don’t get any print statements or errors in the output from this whatsoever, so I have no clue as to why this is happening.
When the client clones a script from replicated storage, the instance will be copied but the source for the script itself wont be which explains why the script isn’t working.
If you change the script to a local script it will work but definitely not the best approach.
It really depends how you want it to work. Do you want it when someone picks up a pumpkin, its removed for everyone or do you want it when someone picks up a pumpkin, than its only removed for that player (but other players can still see and pickup the pumpkin)?
The latter, it’s meant to be a scavenger hunt. So it’d have to be one person can pick it up and others can still see it, while that player could no longer see it.
Actually, what you could do is have everything managed by the server (which is recommended), but when a player touches the pumpkin, the server fires a remote event to that player. The players client can then delete/make the pumpkin invisible on his or her client (but it still exist on the server).
Well, first you would need to create a remoteEvent. You can leave it in replicateStorage and call it whatever.
For this example, I’m going to create a RemoteEvent called “PumpkinEvent” which will be in ReplicatedStorage.
Server Code:
-- This is basically your previous script to detect a part being hit but this time we fire a remote after it gets hit
-- Services --
local PS = game:GetService("Players");
local RS = game:GetService("ReplicatedStorage");
-- Variables --
local PumpkinRemoteEvent = RS:WaitForChild("PumpkinEvent"); -- Wait for the remote event in replicateStorage called PumpkinEvent
local MyPumpkin = script.Parent;
-- Touch Event --
MyPumpkin.Touched:Connect(function(Hit) -- Make sure you add a debounce and basically do what you did in your old script
local Player = PS:GetPlayerFromCharacter(Hit.Parent);
if Player then -- Make sure that its a player
print("Pumpkin Touched by "..Player.Name);
PumpkinRemoteEvent:FireClient(Player, MyPumpkin); -- Use "FireClient" to fire a specific players client. We are also sending MyPumpkin as an argument
-- Do stuff here
end;
end);
Now after we fire the players client. We will need a local script to listen for whenever the PumpkinEvent is fired. So your local script would look something like the following.
Local script:
-- Services --
local RS = game:GetService("ReplicatedStorage");
-- Variables --
local PumpkinRemoteEvent = RS:WaitForChild("PumpkinEvent"); -- Wait for the remote event in replicatedStorage call PumpkinEvent
PumpkinRemoteEvent.OnClientEvent:Connect(function(ThePumpkinToRemove) -- Listen for when the server fires the players client
if ThePumpkinToRemove and ThePumpkinToRemove:IsA("BasePart") then -- Check if the previous argument we sent exist and is a part
ThePumpkinToRemove:Destroy(); -- Destroy the part on the client (but everyone else will still see it on the server)
end;
end);
Thats pretty much all there is to it!! I highly recommend you learn about remote events and remote functions as they are very useful!!
I’ve learned some about events once before, for some reason I just didn’t really get the hang of it. Either way though, thank you for your help! I’ll follow your guidelines and see what happens, if it all goes well I’ll be sure to mark this as the solution.
Hey Ze_tsu, it’s working however I noticed something odd. The player is able to interact with a pumpkin even if they’ve collected it and is destroyed on their end. Would this be because it’s still in the studio workspace? If so, how could I overcome this issue?